Ios 如何在Obj C中的UIImageView顶部绘制横幅
某些上下文 我正在创建一个功能,允许用户将我的应用程序中的一些图像共享到他们的Facebook/Twitter帐户 该功能起作用了,我实现了IBAction,我可以在Facebook和Twitter上分享一些图片 问题 我公司的市场部现在要求我在共享图像的顶部添加一个带有公司徽标和日期的横幅 这是我必须展示的: 现在我只共享白色矩形。现在我必须添加这个横幅,蓝色矩形 我的想法 我想知道我将如何开发它。首先,我考虑在视图中以编程方式绘制它Ios 如何在Obj C中的UIImageView顶部绘制横幅,ios,objective-c,uiimageview,drawrect,Ios,Objective C,Uiimageview,Drawrect,某些上下文 我正在创建一个功能,允许用户将我的应用程序中的一些图像共享到他们的Facebook/Twitter帐户 该功能起作用了,我实现了IBAction,我可以在Facebook和Twitter上分享一些图片 问题 我公司的市场部现在要求我在共享图像的顶部添加一个带有公司徽标和日期的横幅 这是我必须展示的: 现在我只共享白色矩形。现在我必须添加这个横幅,蓝色矩形 我的想法 我想知道我将如何开发它。首先,我考虑在视图中以编程方式绘制它 UIView *banner = [[UIView a
UIView *banner = [[UIView alloc] initWithFrame:CGRectMake(0, 35, self.view.frame.size.width, 60)];
banner.backgroundColor = [UIColor blueColor];
[self.view addSubview:banner];
问题是我真的不知道如何调整它的大小,以适应所有不同的屏幕大小。还有,如何在这个矩形中添加徽标、标签日期,以及最后,如何将其放入共享图像中
然后我想,“也许我可以像Android那样直接在XML文件中绘制它”。但从我所有的调查来看,以编程的方式来做似乎更容易
需要帮助
我希望能给自己一些建议,以便更好地开发它,因为现在我的想法有点模糊
感谢阅读。获得图像后调用此方法
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;
}获得图像后调用此方法
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获得的图像。 这里我有一个横幅视图,你可以添加标签和图像到它 代码:
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();
输出:
请参见灰色条是横幅视图,下面是图像。我没有图像,所以拍摄了模拟器的屏幕截图来演示示例。您可以这样做 假设img1是您从URL获得的图像。 这里我有一个横幅视图,你可以添加标签和图像到它 代码:
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();
输出:
请参见灰色条是横幅视图,下面是图像。我没有图像,因此拍摄了模拟器的屏幕截图来演示示例。因此,您需要在横幅图像的图像上绘制徽标(核心重绘图像,以便图像具有徽标和横幅图像)。或者尝试在图像视图上添加徽标。我已使用快速图形表示法编辑了我的问题,以便您能够理解我的需求。因此,您确切地说,希望在横幅图像上的图像上绘制徽标(核心重绘图像,以便图像具有徽标和横幅图像)。或者尝试在图像视图上添加徽标。我用快速的图形表示法编辑了我的问题,以便您能够理解我的需求。我从来没有提到过拍照。我只共享从服务器检索到的应用程序中的一些图像。所以您将从服务器向类方法获取图像,对吗?是的,我有一个URL,我可以从中获取图像。我目前正在将此图像共享到Facebook/Twitter。现在我只需要添加一个横幅(蓝色矩形),我必须绘制我自己并将其“定位”在顶部,或者创建一个由这两个图像组成的新图像。但我不需要imagePickerController,对吗?那么我为这个[(UIImage*)[info objectForKey:UIImagePickerControllerOriginalImage]drawInRect:CGRectMake(0,0320480)]做了什么@Funnybear你有没有检查过更新后的答案,其中只包括一种方法,在你收到图片后会从你的回复中调用。我从来没有提到过拍照。我只共享从服务器检索到的应用程序中的一些图像。所以您将从服务器向类方法获取图像,对吗?是的,我有一个URL,我可以从中获取图像。我目前正在将此图像共享到Facebook/Twitter。现在我只需要添加一个横幅(蓝色矩形),我必须绘制我自己并将其“定位”在顶部,或者创建一个由这两个图像组成的新图像。但我不需要imagePickerController,对吗?那么我为这个[(UIImage*)[info objectForKey:UIImagePickerControllerOriginalImage]drawInRect:CGRectMake(0,0320480)]做了什么@Funnybear你检查过更新的答案了吗?更新后的答案只包括一个方法,在得到图片后,你的回复会调用这个方法。这正是我要问的。我会接受这个答案,因为它将帮助我走得更远。也感谢@Aneesh。但是是否可以使用图像和横幅创建一个新视图,以便横幅不在图像上,而是在图像上方?是的,这就是我在这里演示的内容。横幅位于imageView上方。在我的例子中,灰色的是添加的图像。我已经将视图的背景色添加为灰色,当你在上面放置图像,然后拍摄图像时,它将根据你的需要来。看,我已经更新了与你的要求基本相似的答案。你是对的,我认为它不在上面,但它在上面。非常感谢。我将开始添加日期和徽标。这正是我所要求的。我会接受这个答案,因为它将帮助我走得更远。也感谢@Aneesh。但是是否可以使用图像和横幅创建一个新视图,以便横幅不在图像上,而是在图像上方?是的,这就是我在这里演示的内容。横幅位于imageView上方。在我的例子中,灰色的是添加的图像。我已经将视图的背景色添加为灰色,当你在上面放置图像,然后拍摄图像时,它将根据你的需要来。看,我已经更新了与你的要求基本相似的答案。你是对的,我认为它不在上面,但它在上面。非常感谢。我将开始添加日期和徽标。