How to merge two UIImages programmatically?

H

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 🙂

About the author

minakshi.bhosale
By minakshi.bhosale

Category