Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge multiple UIImageViews into one UIImage

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?

like image 546
Thomas Joos Avatar asked Dec 01 '22 10:12

Thomas Joos


2 Answers

+ (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; }
like image 122
Andy Milburn Avatar answered Dec 04 '22 13:12

Andy Milburn


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

like image 32
philsquared Avatar answered Dec 04 '22 13:12

philsquared