How to merge two UIImages programmatically?

Hi all,

While developing an application I came to this issue. I was suppose to merge 2 images programmatically.

I googled a little bit and found a function, then I modified it as follows for my requirements

-(UIImage *) mergeTwoImages: (UIImage *)bottomImage : (UIImage *)upperImage : (CGRect)rect

{

UIImage *image = upperImage;

CGSize newSize = CGSizeMake(bottomImage.size.width, bottomImage.size.height);

UIGraphicsBeginImageContext( newSize );

// Use existing opacity as is

[bottomImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

// Apply supplied opacity

[image drawInRect:rect blendMode:kCGBlendModeNormal alpha:1.0];

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return newImage;

}

We just need to pass both the images to be merged and also the co ordinates of the upper image.

That’s it!

Happy Coding 🙂


Need more help?

Hi there, was your problem or query resolved? If not & need more assistance, please do reach out to us at info@nanostuffs.com, we'll be more than delighted to help. Nanostuffs has 7+ years of extensive Salesforce & iOS/Android experience.
Holler Box