在iPhone中将多个图像合并为一个图像

Har*_*ala 8 iphone uiimage ios

我想合并多个不同大小和不同点的图像.
我想将它们全部合并并将其保存在一个副本(图像)中.

那么如何将图像整理成一个图像呢?

Har*_*ala 6

我发现解决方案很简单

您可以通过创建以下方法合并多个图像

- (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;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在以下面的方式调用此方法

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");
}
Run Code Online (Sandbox Code Playgroud)