Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine/ merge 2 images into 1

I would like to know what to do to save 2 images into 1 image.

One of the photos can be moved, rotated and zoomed in/out...

I'm doing this, but it basically captures all the stuff on the screen including my buttons...

UIGraphicsBeginImageContext(self.view.bounds.size); [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *savedImg = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); 
like image 894
Desmond Avatar asked Feb 13 '12 09:02

Desmond


People also ask

How can I put 2 pictures together?

The easiest and most common way to put 2 photos together is a collage. Creating one is an effective way to showcase multiple related photos without posting them all separately. Many people make collages following vacations or significant life events to share images of the memories they made.

Can you merge two pictures together?

Once you've downloaded PicMerger, open up the app and go to the second tab. From here, tap the orange “Stitch Images”. You'll immediately be promoted to select the photos from your camera roll that you want to stitch together. Select the photos you want and tap “Next”.


2 Answers

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; } 
like image 147
Vlad Avatar answered Oct 04 '22 16:10

Vlad


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 UIImageView mergeImage.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 UIImageView imageView.image = finalImage; 
like image 42
Himanshu padia Avatar answered Oct 04 '22 17:10

Himanshu padia