merge multiple UIImages on iOS

I want to merge multiple UIImages in iOS. To do this, I tried to do as below:

UIImage *imageLeft = [UIImage imageNamed:@"ico-left"];
UIImage *imageRight = [UIImage imageNamed:@"ico-right"];
CGSize size = CGSizeMake(imageLeft.size.width + imageRight.size.width, imageLeft.size.height);
[imageLeft drawInRect:CGRectMake(0, 0, imageLeft.size.width, imageLeft.size.height)];
[imageRight drawInRect:CGRectMake(imageLeft.size.width, 0, imageRight.size.width, imageRight.size.height)];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, finalImage.size.width, finalImage.size.height)];
imageView.image = finalImage;
[cell.contentView addSubview:imageView];

But I cannot get any image. How can I fix it? Thanks.


I think you are not adding extension of image (.png) so change your upper two lines by these

    UIImage *imageLeft = [UIImage imageNamed:@"ico-left.png"];
    UIImage *imageRight = [UIImage imageNamed:@"ico-right.png"];

I am not sure whether this is your problem but you can give it a try. Sometimes it worked for me

NSString *path1 = [[NSBundle mainBundle] pathForResource:@"ico-left" ofType:@"png"];
NSString *path2 = [[NSBundle mainBundle] pathForResource:@"ico-right" ofType:@"png"];
UIImage *imageLeft = [[UIImage alloc] initWithContentsOfFile:path1];
UIImage *imageRight = [[UIImage alloc] initWithContentsOfFile:path2];

Here's the code I use to merge images. (I think I found all or part of it online at some point). Put it in a UIImage category.

    // NOTE! this method should only be called from the main thread because of
    // UIGraphicsGetImageFromCurrentImageContext();
    - (UIImage *)merge:(UIImage *)image atRect:(CGRect)pos overlay:(BOOL)overlay fillColor:(UIColor *)fill 

        UIImage *bottom = (overlay)?self:image;
        UIImage *top = (overlay)?image:self;

        CGRect lf = CGRectMake(0, 0, self.size.width, self.size.height);

        CGRect bottomRect = (overlay)?lf:pos;
        CGRect topRect = (overlay)?pos:lf;

        if (fill){
            [fill setFill];
            CGContextFillRect (UIGraphicsGetCurrentContext(), topRect);

        [bottom drawInRect:bottomRect];
        [top drawInRect:topRect];

        UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext();    
        return destImage;   


Need Your Help

Remove the 1px border under UISearchBar

objective-c xcode uiview uikit uisearchbar

I'm having trouble removing that 1px border directly under the UISearchBar view. Now it doesn't look matched up with my done button view. I've tried: