Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - Merging two images of different size

I'm facing the following problem : I have to merge two images A and B to create a new image C as a result of the merging.
I already know how to merge two images but in this case my goal is a little bit different.
I would like that image A will be the background for Image B.
For instance if image A size is 500x500 and image B size is 460x460 I would like that image C (the image result of the merging) will be 500x500, with image B (460x460) centered in it.

Thanks in advance for any help or suggestion

like image 225
oiledCode Avatar asked Feb 09 '12 10:02

oiledCode


2 Answers

This is what I've done in my app, but without using UIImageView:

UIImage *bottomImage = [UIImage imageNamed:@"bottom.png"]; //background image UIImage *image       = [UIImage imageNamed:@"top.png"]; //foreground image  CGSize newSize = CGSizeMake(width, height); UIGraphicsBeginImageContext( newSize );  // Use existing opacity as is [bottomImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];  // Apply supplied opacity if applicable [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:0.8];  UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();  UIGraphicsEndImageContext(); 

If the image already has opacity, you do not need to set it (as in bottomImage) otherwise you can set it (as with image).

After this UIImage is created then you can embed it in your UIImageView

UPDATE: Thanks to Ahmet AkkoK - for Swift (2.2) users blend mode macro has changed. CGBlendMode .kCGBlendModeNormal is replaced with CGBlendMode.Normal

like image 176
Srikar Appalaraju Avatar answered Sep 22 '22 19:09

Srikar Appalaraju


Hey i got multiple images add same background with different foreground This is my code

UIImage *bottomImage = [UIImage imageNamed:@"photo 2.JPG"]; //background image UIImage *image       = [UIImage imageNamed:@"photo 3.JPG"]; //foreground image UIImage *image1      = [UIImage imageNamed:@"photo 4.JPG"]; //foreground image UIImage *image2      = [UIImage imageNamed:@"photo 5.JPG"]; //foreground image  CGSize newSize = CGSizeMake(320, 480); UIGraphicsBeginImageContext( newSize );  // Use existing opacity as is [bottomImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];  // Apply supplied opacity if applicable [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:0.4]; [image1 drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:0.3]; [image2 drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:0.2]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();  UIGraphicsEndImageContext();  resultView = [[UIImageView alloc] initWithImage:newImage]; resultView.frame = CGRectMake(0, 0,320,460); [self.view addSubview:resultView]; 
like image 34
USK Avatar answered Sep 18 '22 19:09

USK