Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging two UIImage into 1 to be savedtolibrary

Tags:

ios

uiimage

I would like to know how can I merge 2 uiimage into 1? I would like to save the end product to the library. For saving images I'm using a UI button. Here's snippet of how I save a UIImageview.image.

-(IBAction)getPhoto:(id)sender  {

UIImage* imageToSave = imageOverlay.image;

UIImageWriteToSavedPhotosAlbum(imageToSave, nil, nil, nil);
}

I've looked online and read about UIGraphicsBeginImageContext. Found an example but I couldn't understand how to actually apply it to mine. Here's the one I've got so far.

- (UIImage*)addImage:(UIImage *)image secondImage:(UIImage *)image2 
{
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
[image2 drawInRect:CGRectMake(10,10,image2.size.width,image2.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return newImage;
}

Right now I have 2 UIImageviews which is imageOverlay.image and imageView.image. If I use the method above how to assign the return value to UIImageWriteToSavedPhotoAlbum? Hope someone can point me to the right direction.

Thanks very much.

like image 297
hakimo Avatar asked Jul 14 '11 08:07

hakimo


2 Answers

create a NSLog, and pass them through it like this, image1 and image2 are your images:

NSLog (@"testing images aren't nil. Details for image1: %@ image2: %@",image1,image2);

I'm a newbie too, and I don't really know a more sophisticated way to see if the image is nil, but I saw that by passing a nil image to the statement above, you'd only get nil instead of the values of those two images. If you get lots of information, even though they're alot of codes and you may not understand them either, as I usually don't, at least you can test whether they are nil or not.

like image 79
Septronic Avatar answered Nov 19 '22 01:11

Septronic


Try this:

-(IBAction)getPhoto:(id)sender  {

UIImage* imageToSave = [self addImage:imageOverlay.image secondImage:imageView.image];

UIImageWriteToSavedPhotosAlbum(imageToSave, nil, nil, nil);
}
like image 1
Daniel Avatar answered Nov 18 '22 23:11

Daniel