2012-08-31 30 views
5

我正在開發一個iPhone應用程序,我想合併兩個UIImage(leftImage和rightImage)。任何人都可以建議如何做到這一點,而沒有在他們之間的線?如何合併iPhone SDK中Objective-C中的兩個UIImage對象

我目前這樣做:

- (UIImage*)mergeImage:(UIImage*)first withImage:(UIImage*)second 
{ 
    // get size of the first image 
    CGImageRef firstImageRef = first.CGImage; 
    CGFloat firstWidth = CGImageGetWidth(firstImageRef); 
    CGFloat firstHeight = CGImageGetHeight(firstImageRef); 

    // get size of the second image 
    CGImageRef secondImageRef = second.CGImage; 
    CGFloat secondWidth = CGImageGetWidth(secondImageRef); 
    CGFloat secondHeight = CGImageGetHeight(secondImageRef); 

    // build merged size 
    CGSize mergedSize = CGSizeMake((firstWidth+secondWidth), MAX(firstHeight, secondHeight)); 

    // capture image context ref 
    UIGraphicsBeginImageContext(mergedSize); 

    //Draw images onto the context 
    [first drawInRect:CGRectMake(0, 0, firstWidth, firstHeight)]; 
    //[second drawInRect:CGRectMake(firstWidth, 0, secondWidth, secondHeight)]; 
    [second drawInRect:CGRectMake(firstWidth, 0, secondWidth, secondHeight) blendMode:kCGBlendModeNormal alpha:1.0]; 

    // assign context to new UIImage 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 

    // end context 
    UIGraphicsEndImageContext(); 

    return newImage; 

} 

但它創造了兩個圖像之間的小行。我不想要這個分隔符,就像Splitcam那樣。

我該怎麼做?

回答

6

只有邊緣重疊一個像素。

[second drawInRect:CGRectMake(firstWidth-1, 0, secondWidth, secondHeight) 
    blendMode:kCGBlendModeNormal alpha:1.0]; 
+0

它不工作仍然給他們之間的界限。 –