将两张图像混合在一起

时间:2016-04-08 05:07:45

标签: ios objective-c core-image

我正在尝试将两个图像合并到最终图像中,但遗憾的是我找不到这样做的方法。如何合并两张照片如下图所示?

enter image description here

enter image description here

enter image description here

4 个答案:

答案 0 :(得分:2)

UIImage *image1 = [UIImage imageNamed:@"image1.jpg"];
UIImage *image2 = [UIImage imageNamed:@"image2.jpg"];

CGSize newSize = image1.size; //set new size as you want
UIGraphicsBeginImageContext( newSize );

//crop image1
CGImageRef imageRef = CGImageCreateWithImageInRect([image1 CGImage], CGRectMake(0, 0, image1.size.width/2, image1.size.height));
image1 = [UIImage imageWithCGImage:imageRef scale:image1.scale orientation:image1.imageOrientation];
CGImageRelease(imageRef);


//crop image2
imageRef = CGImageCreateWithImageInRect([image2 CGImage], CGRectMake(0, 0, image2.size.width/2, image2.size.height));
image2 = [UIImage imageWithCGImage:imageRef scale:image2.scale orientation:image2.imageOrientation];
CGImageRelease(imageRef);

//combine both images
// Use existing opacity as is
[image1 drawInRect:CGRectMake(0, 0, newSize.width/2, newSize.height)];

// Apply supplied opacity if applicable
[image2 drawInRect:CGRectMake(newSize.width/2, 0, newSize.width/2, newSize.height) blendMode:kCGBlendModeNormal alpha:1];

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

NSString *strPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true) objectAtIndex:0];
strPath = [strPath stringByAppendingPathComponent:@"img.png"];
NSError *error = nil;
[[NSFileManager defaultManager] removeItemAtPath:strPath error:&error];

NSData *imgdata = UIImagePNGRepresentation(newImage);
[imgdata writeToFile:strPath atomically:false];
NSLog(@"Path = %@",strPath); //on this path image will be stored

答案 1 :(得分:2)

更简单,您可以使用CISwipeTransition并将inputTime设为0.5,

西蒙

答案 2 :(得分:1)

如果要使用Core Graphics,可以创建渐变蒙版,绘制第一个图像,将渐变应用为剪切蒙版,然后绘制第二个图像。因此,第一个图像未被剪切,第二个图像被剪切为渐变:

- (UIImage *)imageCombiningImage:(UIImage *)image1 withImage:(UIImage *)image2 {
    CGRect rect = CGRectMake(0, 0, image1.size.width, image1.size.height);

    UIGraphicsBeginImageContextWithOptions(image1.size, TRUE, image1.scale);

    // create gradient

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGFloat locations[] = { 0.45, 0.55 };  // gradient goes from 45% to 55% the way across the image
    CGFloat components[] = {
        1.0, 1.0, 1.0, 1.0,                // Start color ... white
        0.0, 0.0, 0.0, 0.0                 // End color   ... clear
    };

    CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, components, locations, 2);

    // create mask from that gradient

    CGContextRef context = UIGraphicsGetCurrentContext();        
    CGContextDrawLinearGradient(context, gradient, CGPointMake(0, 0), CGPointMake(image1.size.width, 0), kCGGradientDrawsAfterEndLocation);
    CGImageRef gradientImageRef = CGBitmapContextCreateImage(context);

    // draw the first image

    [image1 drawInRect:rect];

    // clip subsequent drawing to the gradient mask we just created

    CGContextClipToMask(context, rect, gradientImageRef);

    // draw the second image

    [image2 drawInRect:rect];

    // extract the image

    UIImage *combinedImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    // clean up

    CGColorSpaceRelease(colorSpace);
    CGGradientRelease(gradient);
    CGImageRelease(gradientImageRef);

    return combinedImage;
}

结果是:

enter image description here

答案 3 :(得分:0)

_View1.frame = CGRectMake(0, 0, Width/2, Width);
_View2.frame = CGRectMake(0, 0, Width/2, Width);

    CAGradientLayer *gradientMask = [CAGradientLayer layer];
    gradientMask.frame = self.View2.bounds;
    gradientMask.colors = @[(id)[UIColor whiteColor].CGColor,
                            (id)[UIColor clearColor].CGColor];
    gradientMask.startPoint = CGPointMake(0.5, 0.5);   // start at left middle
    gradientMask.endPoint = CGPointMake(0, 0.5);     // end at right middle
    self.View2.layer.mask = gradientMask;

=> I got help from this answer.