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

时间:2014-01-02 07:06:21

标签: ios iphone uiimage

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

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

3 个答案:

答案 0 :(得分: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;
    }
}

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

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");
}

答案 1 :(得分:1)

你可以使用CIImage,

-(UIImage *)mergeTwoImage:(UIImage*)image1 andImage2:(UIImage *)image2
{

    CIImage *topImage = [[CIImage alloc]initWithImage:image1];
    CIImage *backgroundImage = [[CIImage alloc]initWithImage:image2];
    CIFilter *darkenFilter = [CIFilter filterWithName:@"CIDarkenBlendMode" keysAndValues:kCIInputImageKey,topImage,
                          @"inputBackgroundImage",backgroundImage,nil];

    CIImage *resultOfFilter = darkenFilter.outputImage;

    CIContext *ctx = [CIContext contextWithOptions:nil];
    CGImageRef imageToReturn = [ctx createCGImage:resultOfFilter fromRect:resultOfFilter.extent];

    UIImage *outputImage = [UIImage imageWithCGImage:imageToReturn];
    CGImageRelease(imageToReturn);
    imageToReturn = nil;

    return outputImage;
}

答案 2 :(得分:0)

您可以使用此代码: -

    - (IBAction) mergeImage:(id)sender
{
            UIImage *framedImage = firstImageView.image;

            CGSize size = CGSizeMake(firstImageView.frame.size.width, firstImageView.frame.size.height);

            UIGraphicsBeginImageContextWithOptions(size, self.view.alpha, 0.0);

            UIImage *imageToPlace = secondImageView.image;

            [framedImage drawInRect:CGRectMake(0, 0, firstImageView.frame.size.width, firstImageView.frame.size.height)];

            [imageToPlace drawInRect:CGRectMake(18, 25, 242, 243)]; 

            UIImage *imageC = UIGraphicsGetImageFromCurrentImageContext();

            UIGraphicsEndImageContext();

            UIImageView *newImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, framedImage.size.width,framedImage.size.height)];

            newImageView.image = imageC;

            newImageView.contentMode = UIViewContentModeScaleAspectFill;
}

这是2个图像,它们将两个UIImageViews合并为一个。对于多个图像也是如此。您必须根据您的尺寸合并多个图像。