【问题标题】:Mix two images together将两个图像混合在一起
【发布时间】:2016-04-10 07:31:40
【问题描述】:

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

【问题讨论】:

标签: ios objective-c core-image


【解决方案1】:
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

【讨论】:

  • 感谢@Richard G 的帮助
  • 这显然不会渲染两幅图像之间的渐变过渡,而是一个硬过渡。
【解决方案2】:

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

西蒙

【讨论】:

  • 如何使用 CISwipeTransition?你有什么例子吗?
  • 滑动过渡接受两个图像 - inputImageinputTargetImage。它主要用于在幻灯片放映等应用程序中的图像之间转换,但通过将inputTime 设置为 0.5,它可以提供两个图像的分屏视图。恐怕我不会说 ObjC,但我确实有一个用于探索 CI 转换的 Swift 演示应用程序:github.com/FlexMonkey/CoreImageTransitionExplorer
  • 没错。但我只需要调整图像之间的混合渐变,我不需要过渡。之后我必须将这两个图像绘制成一个图像。
【解决方案3】:

如果你想使用 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;
}

结果:

【讨论】:

    【解决方案4】:
    _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.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-06
      • 1970-01-01
      • 1970-01-01
      • 2017-01-02
      • 1970-01-01
      • 2013-03-05
      • 1970-01-01
      相关资源
      最近更新 更多