Merge multiple images into one image in iPhone
ios, iphone, uiimage
Solution
I found the solution which is so simple
you can merge multiple images by creating following method
- (BOOL) mergedImageOnMainImage:(UIImage *)mainImg WithImageArray:(NSArray *)imgArray AndImagePointArray:(NSArray *)imgPointArray
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
UIGraphicsBeginImageContext(mainImg.size);
[mainImg drawInRect:CGRectMake(0, 0, mainImg.size.width, mainImg.size.height)];
int i = 0;
for (UIImage *img in imgArray) {
[img drawInRect:CGRectMake([[imgPointArray objectAtIndex:i] floatValue],
[[imgPointArray objectAtIndex:i+1] floatValue],
img.size.width,
img.size.height)];
i+=2;
}
CGImageRef NewMergeImg = CGImageCreateWithImageInRect(UIGraphicsGetImageFromCurrentImageContext().CGImage,
CGRectMake(0, 0, mainImg.size.width, mainImg.size.height));
UIGraphicsEndImageContext();
[pool release];
if (NewMergeImg == nil) {
return NO;
}
else {
UIImageWriteToSavedPhotosAlbum([UIImage imageWithCGImage:NewMergeImg], self, nil, nil);
return YES;
}
}
now call this method in follwing way
NSArray *imgArray = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:@"image06.png"],
[UIImage imageNamed:@"image07.png"],
[UIImage imageNamed:@"image08.png"],
[UIImage imageNamed:@"image09.png"],
[UIImage imageNamed:@"BackBtn.png"],
[UIImage imageNamed:@"Facebook.png"], nil];
NSArray *imgPointArray = [[NSArray alloc] initWithObjects:
@"10", @"10",
@"10", @"25",
@"30", @"15",
@"30", @"50",
@"20", @"80",
@"25", @"100", nil];
BOOL suc = [self mergedImageOnMainImage:[UIImage imageNamed:@"img001.png"] WithImageArray:imgArray AndImagePointArray:imgPointArray];
if (suc == YES) {
NSLog(@"Images Successfully Mearged & Saved to Album");
}
else {
NSLog(@"Images not Mearged & not Saved to Album");
}
Problem
I want to merge multiple images which are of different size & at different points. I want to merge them all and save it in one copy(image). so how can i mearge images into one image?