How to merge multiple UIImageViews into one UIImage

安稳与你 提交于 2019-12-09 13:19:15

问题


I have 2 UIImageViews laying on top of each other ( picture + overlay frame ) and I want to save them to the camera roll as 1 picture.

How should I combine those 2 UIImageViews so that I can call the UIImageWriteToSavedPhotosAlbum function, using a 'result' UIImage?


回答1:


I don't have my Mac nearby at the moment, but I have done this before. The process is that you'll render the UIImageViews into a bitmap context, then create a CGImage from that context that you can use to create a new UIImage from. Read up on CGBitmapContext, including CGBitmapContextCreateImage




回答2:


+ (UIImage * ) mergeImage: (UIImage *) imageA
                                 withImage:  (UIImage *) imageB
                                 strength: (float) strength {

UIGraphicsBeginImageContextWithOptions(CGSizeMake(imageA.size.width, imageA.size.height), YES, 0.0); 

[imageA drawAtPoint: CGPointMake(0,0)];

[imageB drawAtPoint: CGPointMake(0,0) 
          blendMode: kCGBlendModeNormal // you can play with this
              alpha: strength]; // 0 - 1

UIImage *answer = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext();
return answer; }



回答3:


To merge two images in one image use the following code:

- (UIImage * ) mergeImage: (UIImage *) imageA withImage:  (UIImage *) imageB
{

UIGraphicsBeginImageContextWithOptions(CGSizeMake(imageA.size.width, imageA.size.height), YES, 0.0);

[imageA drawAtPoint: CGPointMake(0,0)];

[imageB drawAtPoint: CGPointMake(0,0)
          blendMode: kCGBlendModeNormal // you can play with this
              alpha: 1]; // 0 - 1

UIImage *answer = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return answer;

}


来源:https://stackoverflow.com/questions/2560082/how-to-merge-multiple-uiimageviews-into-one-uiimage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!