加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iphone – 如何将UIImage和UILabel组合成一个图像并保存

发布时间:2020-12-14 19:51:44 所属栏目:百科 来源:网络整理
导读:我有2个UILabels和2个图像,我需要合并成一个UI Image保存. 我知道我可以用屏幕截图进行拍摄,但是我的主要图像是圆形的,所以如果我反过来,它仍然会显示出尖锐的边缘. 我可以这样做来组合图像: //CGSize newImageSize = CGSizeMake(cropImage.frame.size.widt
我有2个UILabels和2个图像,我需要合并成一个UI Image保存.

我知道我可以用屏幕截图进行拍摄,但是我的主要图像是圆形的,所以如果我反过来,它仍然会显示出尖锐的边缘.

我可以这样做来组合图像:

//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.

任何回复都非常感激.

解决方法

使用[myLabel.layer renderInContext:UIGraphicsGetCurrentContext()];在当前的背景下绘制.

例如:

UIGraphicsBeginImageContextWithOptions(newImageSize,0.0); //retina res
    [self.viewForImg.layer renderInContext:UIGraphicsGetCurrentContext()];
    [myLabel.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

根据你的意见,如果你想在一个特定的框架中绘制它,如下所示,

[myLabel drawTextInRect:CGRectMake(0.0f,0.0f,100.0f,50.0f)];

如果要背景颜色,请尝试一下,

CGRect drawRect = CGRectMake(rect.origin.x,rect.origin.y,rect.size.width,rect.size.height); 
CGContextSetRGBFillColor(context,100.0f/255.0f,1.0f); 
CGContextFillRect(context,drawRect);

或者你可以检查这个问题Setting A CGContext Transparent Background.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读