2016-04-08 126 views
0

我想將兩個圖像合併到最終圖像中,但不幸的是我無法找到方法來做到這一點。如何合併兩張照片,如下所示?將兩個圖像混合在一起

enter image description here

enter image description here

enter image description here

+1

本教程可以幫助https://www.raywenderlich.com/69855/image-processing-in-ios-part-1-raw-bitmap-modification –

+0

如何使用Photoshop? –

回答

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 
+0

感謝您的幫助@Richard G –

+0

歡迎您:) –

+0

這顯然不會呈現兩個圖像之間的漸變過渡,而是一個艱難的過渡。 – Rob

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.

2

就更簡單了,你可以使用一個CISwipeTransition,並設置inputTime 0.5,

西蒙

+0

我如何處理CISwipeTransition?你有什麼例子嗎? –

+0

滑動轉換接受兩個圖像 - 「inputImage」和「inputTargetImage」。它主要是爲幻燈片放映等應用程序中的圖像之間的轉換而設計的,但通過將'inputTime'設置爲0.5,可以對兩幅圖像進行分屏顯示。恐怕我不會說ObjC,但我確實有一個Swift演示應用程序用於探索CI轉換:https://github.com/FlexMonkey/CoreImageTransitionExplorer –

+0

沒錯。但我只需要調整圖像之間的混合漸變,我不需要過渡。之後我必須將這兩幅圖像繪製成一幅圖像。 –

1

如果你想使用核芯顯卡,您可以創建一個梯度面具,繪製的第一個映像,應用梯度剪貼蒙版,然後繪製第二圖像。因此,第一圖像是未截短的,第二個被限幅到梯度:

- (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