如何在 Obj C 中的 UIImageView 顶部绘制横幅

How can I draw a banner at the top of a UIImageView in Obj C

一些上下文

我正在创建一项功能,使用户能够将我的应用程序中的一些图片分享到他们的 Facebook/Twitter 帐户。

该功能有效,我实现了 IBAction,我可以在 Facebook 和 Twitter 中分享一些图像。

问题

我公司的营销部门现在要求我在共享图片的顶部添加一个带有公司徽标和日期的横幅。

这是我要显示的内容:

现在我只分享白色矩形。现在我必须添加这个横幅,蓝色矩形。

我的想法

我想知道我将如何开发它。首先,我正在考虑在视图中以编程方式绘制它。

UIView *banner  = [[UIView alloc] initWithFrame:CGRectMake(0, 35, self.view.frame.size.width, 60)];
banner.backgroundColor = [UIColor blueColor];
[self.view addSubview:banner];

问题是我真的不知道如何调整它的大小以适应所有不同的屏幕尺寸。而且,如何在此矩形内添加徽标、标签日期以及最后,如何将其放入共享的 UIImage

然后我在想,"maybe I can draw it like in Android, directly in the XML file"。但从我所有的调查来看,以编程方式进行看起来更容易。

需要帮助

我希望得到一些建议,以便专注于更好的开发方法,因为现在我的想法有点模糊。

感谢您的阅读。

 UIView *banner  = [[UIView alloc] initWithFrame:CGRectMake(0, 35, self.view.frame.size.width, 60)];
banner.backgroundColor = [UIColor blueColor];


UIImageView *imgBanner=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, banner.frame.size.width, banner.frame.size.height)];
    [imgBanner setImage:@"img.png"];
     imgBanner.contentMode = UIViewContentModeScaleAspectFit;

[banner addSubView:imgBanner];
[self.view addSubview:banner];

获取图片后调用此方法,

UIImage *img = [self drawText:@"Some text"
                            inImage:img 
                            atPoint:CGPointMake(0, 0)];

这样的函数实现,

+(UIImage*) drawText:(NSString*) text 
             inImage:(UIImage*)  image 
             atPoint:(CGPoint)   point 
{

UIFont *font = [UIFont boldSystemFontOfSize:12];
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
CGRect rect = CGRectMake(point.x, point.y, image.size.width, image.size.height);
[[UIColor whiteColor] set];
[text drawInRect:CGRectIntegral(rect) withFont:font]; 
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return newImage;

}

你可以这样做

假设 img1 是您从 URL 获得的图像。 这里我取了一个bannerView,你可以把标签和图片加进去。

代码:

UIImage *img1 = [UIImage imageNamed:@"Screen.png"];

//Only banner view, on which we will design the label and logo
UIView *vwBanner = [[UIView alloc] initWithFrame:CGRectMake(0, 0, img1.size.width, 60)];
vwBanner.backgroundColor = [UIColor grayColor];

//Logo design
UIImageView *imgVwLogo = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(vwBanner.frame) - 100 , 0, 100, 100)];
imgVwLogo.backgroundColor = [UIColor redColor];
[vwBanner addSubview:imgVwLogo];

//Label design
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, CGRectGetWidth(vwBanner.frame) - imgVwLogo.frame.size.width, CGRectGetHeight(vwBanner.frame))];
lbl.text = @"Thurseday, 20 October 2016";
[vwBanner addSubview:lbl];


//Full view which contains the downloaded image and banner image
UIView *vwWithBanner = [[UIView alloc] initWithFrame:CGRectMake(0, 0, img1.size.width, img1.size.height+vwBanner.frame.size.height)];
[vwWithBanner addSubview:vwBanner];

//imageView with downloaded image 
UIImageView *imgVw = [[UIImageView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(vwBanner.frame), img1.size.width, img1.size.height)];
imgVw.image = img1;
[vwWithBanner addSubview:imgVw];

//Draw the image of full view
UIGraphicsBeginImageContextWithOptions(vwWithBanner.bounds.size, NO, img1.scale);
[vwWithBanner drawViewHierarchyInRect:vwWithBanner.bounds afterScreenUpdates:YES];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

输出:

看到灰色条是横幅视图,下面是图像。我没有图片所以拍了模拟器的屏幕截图来演示这个例子。