How to combine UIImage and UILabel into one image and save(如何将 UIImage 和 UILabel 合并为一张图片并保存)
问题描述
我有 2 个 UILabel 和 2 个图像需要合并到一个 UIImage 中保存.
I have 2 UILabels and 2 images that i need to merge into a single UIImage to save.
我知道我可以通过屏幕截图来做到这一点,但我的主图像是圆形的,所以如果我把它矫正,它仍然会显示出锐利的边缘.
I know I could do it with screen shots but my main image is rounded so if I rect it, it will still show the sharp edge.
我可以这样做来合并图像:
I can do this to combine the images :
//CGSize newImageSize = CGSizeMake(cropImage.frame.size.width, cropImage.frame.size.height);
CGSize newImageSize = CGSizeMake(480, 320);
NSLog(@"CGSize %@",NSStringFromCGSize(newImageSize));
UIGraphicsBeginImageContextWithOptions(newImageSize, NO, 0.0); //retina res
[self.viewForImg.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
NSData *imgData = UIImageJPEGRepresentation(image, 0.9); //UIImagePNGRepresentation ( image ); // get JPEG representation
UIImage * imagePNG = [UIImage imageWithData:imgData]; // wrap UIImage around PNG representation
UIGraphicsEndImageContext();
return imagePNG;
但不确定如何在 UILabel 中添加.
but not sure how to add in the UILabel.
非常感谢任何回复.
推荐答案
使用 [myLabel.layer renderInContext:UIGraphicsGetCurrentContext()];
在当前上下文中绘制.
Use [myLabel.layer renderInContext:UIGraphicsGetCurrentContext()];
to draw in current context.
例如:-
UIGraphicsBeginImageContextWithOptions(newImageSize, NO, 0.0); //retina res
[self.viewForImg.layer renderInContext:UIGraphicsGetCurrentContext()];
[myLabel.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
根据您的评论,如果您想在特定框架中绘制此内容,请按以下步骤操作,
Based on your comments, if you want to draw this in a particular frame do it as follows,
[myLabel drawTextInRect:CGRectMake(0.0f, 0.0f, 100.0f, 50.0f)];
如果你想给背景上色,试试这个,
If you want to color the background, try this,
CGRect drawRect = CGRectMake(rect.origin.x, rect.origin.y,rect.size.width, rect.size.height);
CGContextSetRGBFillColor(context, 100.0f/255.0f, 100.0f/255.0f, 100.0f/255.0f, 1.0f);
CGContextFillRect(context, drawRect);
或者你可以检查这个问题Setting A CGContext Transparent Background.
or you can check this question Setting A CGContext Transparent Background.
这篇关于如何将 UIImage 和 UILabel 合并为一张图片并保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将 UIImage 和 UILabel 合并为一张图片并保存
基础教程推荐
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01