Satish Azad
Satish Azad

Reputation: 2312

How to merge two UIImage object in Objective-C in iPhone SDK

I am developing an iPhone Application, and I want to merge two UIImages (leftImage and rightImage). Can anyone suggest how to do that without having lines in 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 the two images. I don't want this separator, just like how Splitcam does it.

How can I do this ?

Upvotes: 5

Views: 2913

Answers (1)

Mundi
Mundi

Reputation: 80265

Just have the edges overlap by one pixel.

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

Upvotes: 6

Related Questions