How to combine/ merge 2 images into 1
You can create graphics context and draw both images in it. You'll get an image result from both your source images combined.
- (UIImage*)imageByCombiningImage:(UIImage*)firstImage withImage:(UIImage*)secondImage { UIImage *image = nil; CGSize newImageSize = CGSizeMake(MAX(firstImage.size.width, secondImage.size.width), MAX(firstImage.size.height, secondImage.size.height)); if (UIGraphicsBeginImageContextWithOptions != NULL) { UIGraphicsBeginImageContextWithOptions(newImageSize, NO, [[UIScreen mainScreen] scale]); } else { UIGraphicsBeginImageContext(newImageSize); } [firstImage drawAtPoint:CGPointMake(roundf((newImageSize.width-firstImage.size.width)/2), roundf((newImageSize.height-firstImage.size.height)/2))]; [secondImage drawAtPoint:CGPointMake(roundf((newImageSize.width-secondImage.size.width)/2), roundf((newImageSize.height-secondImage.size.height)/2))]; image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image;}
Swift 4.2
let topImage = UIImage(named: "image1.png")let bottomImage = UIImage(named: "image2.png")let size = CGSize(width: topImage!.size.width, height: topImage!.size.height + bottomImage!.size.height)UIGraphicsBeginImageContextWithOptions(size, false, 0.0)topImage!.draw(in: CGRect(x: 0, y: 0, width: size.width, height: topImage!.size.height))bottomImage!.draw(in: CGRect(x: 0, y: topImage!.size.height, width: size.width, height: bottomImage!.size.height))let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!UIGraphicsEndImageContext()//set finalImage to IBOulet UIImageViewmergeImage.image = newImage
Objective-C
UIImage *image1 = [UIImage imageNamed:@"image1.png"];UIImage *image2 = [UIImage imageNamed:@"image2.png"];CGSize size = CGSizeMake(image1.size.width, image1.size.height + image2.size.height);UIGraphicsBeginImageContext(size);[image1 drawInRect:CGRectMake(0,0,size.width, image1.size.height)];[image2 drawInRect:CGRectMake(0,image1.size.height,size.width, image2.size.height)];UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();//set finalImage to IBOulet UIImageViewimageView.image = finalImage;
- Create a subview for adding images.
- Add all your images in that view instead of the main view.
- Let the buttons and other stuff stay on the main view.
- Render only the view with images in the bitmap context instead of the main view like you are doing right now.