Merging two UIImage into 1 to be savedtolibrary
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;
UIImageWr开发者_高级运维iteToSavedPhotosAlbum(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.
Seems like you don't have the header for the method in your header file, in the .h file of the class/view controller from which you're running the IBAction, add the first line of your method, -(UIImage*) addImage:... And end it with a ; before the @end. That should work, though you'd have to implement it in the .m file of the same .h file somewhere.
Try this:
-(IBAction)getPhoto:(id)sender {
UIImage* imageToSave = [self addImage:imageOverlay.image secondImage:imageView.image];
UIImageWriteToSavedPhotosAlbum(imageToSave, nil, nil, nil);
}
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.
精彩评论