簡體   English   中英

將兩個圖像混合在一起

[英]Mix two images together

我正在嘗試將兩個圖像合並到最終圖像中,但是不幸的是,我找不到辦法。 如何合並如下所示的兩張照片?

在此處輸入圖片說明

在此處輸入圖片說明

在此處輸入圖片說明

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

更簡單的是,您可以使用CISwipeTransition並將inputTime設置為0.5,

西蒙

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

結果是:

在此處輸入圖片說明

_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;

=> 我從這個答案中得到了幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM