How to combine two UIImage objects in Objective-C in iPhone SDK

I am developing an application for the iPhone and I want to combine the two UIImages (leftImage and rightImage). Can anyone suggest how to do this without having lines between them?

I am currently doing this:

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

}

But it creates a small line between two images. I do not need this separator, as Splitcam does .

How can i do this?

+5
source share
1 answer

Just overlay the edges with one pixel.

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

All Articles