0

Possible duplicate : Saving two Overlapping UIImage

In a UIScrollView, i've added my UIImageView and there's a frame Layer above it. I want to save the final Image on screen after all editing. Answer given to the above mentioned question does the work but as it's drawing the frame, results in degradation of quality of my image , So i'm searching for a solution which keeps my image in good resolution. Please help me out of this, thanks in advance !

Community
  • 1
  • 1
nickAtStack
  • 163
  • 8

1 Answers1

1

Try with this code:

  //merge two images for this code

 UIImage *bottomImage =imgview.image; //background image ////1st image
 UIImage *image       = imgProfile.image; //foreground image///2nd image

 CGSize newSize = CGSizeMake(270, 330); // set your image rect
 UIGraphicsBeginImageContext( newSize );

 // Use existing opacity as is
 [bottomImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];///1st image set frame

 // Apply supplied opacity if applicable

 [image drawInRect:CGRectMake(81,218,97,78) blendMode:kCGBlendModeNormal alpha:1]; //2nd image set frame on bottom image with alpha value

 UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

 UIGraphicsEndImageContext();
 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths objectAtIndex:0];
 NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];
 NSData *imageData = UIImagePNGRepresentation(newImage);
 [imageData writeToFile:savedImagePath atomically:NO];

You can see your newly created image in your application's document directory.

Pradhyuman sinh
  • 3,936
  • 1
  • 23
  • 38
  • i didn't get it sorry, why you've used these two statements: CGSize newSize = CGSizeMake(270, 330); [image drawInRect:CGRectMake(81,218,97,78) blendMode: kCGBlendModeNormal alpha:1]; and how will it manage manage to keep my image in original resolution ? could u explain please – nickAtStack Nov 18 '13 at 07:40
  • first CGSize Newsize it defines whole size image's size and second one frame is the frame for second image. Means where you want to display second image on bottom image. In some case foreground image is small then bottom image so if you want to display small image over big image at top or bottom or in center. As per your requirement set second image's frame over first image – Pradhyuman sinh Nov 18 '13 at 08:42
  • but as i said, it doesn't maintain the actual resolution of my image, it'll result in an image of 270X330.. :( – nickAtStack Nov 19 '13 at 06:12
  • then set your own resolution inplace of 270 x 330. – Pradhyuman sinh Nov 19 '13 at 06:14
  • but i can't set it more than 320X480 still.. that's the sad part – nickAtStack Nov 19 '13 at 06:22
  • Try with replacing UIGraphicsBeginImageContext( newSize ); with if (UIGraphicsBeginImageContextWithOptions != NULL) { UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0.0f); } else { UIGraphicsBeginImageContext(self.view.bounds.size); } – Pradhyuman sinh Nov 19 '13 at 06:31
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/41454/discussion-between-pradhyuman-chavda-and-nickatstack) – Pradhyuman sinh Nov 19 '13 at 06:36