Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating single image by combining more than one images in iOS

I am using UIImageView and I have to set more than one image as a background.

All the images have transparent background and contains any one symbol at their corners. Images are saved based on the conditions. Also there is possibility that there can be more than one images too.

Currently I am setting images, but I can view only the last image. So I want that all the images should be displayed together.

Please do let me know if there is any other way through which I can convert multiple images into single image.

Any help will be appreciated

Thanks in advance

like image 365
Ankita Shah Avatar asked Feb 07 '14 04:02

Ankita Shah


3 Answers

You can draw the images with blend modes. For example, if you have a UIImage, you can call drawAtPoint:blendMode:alpha:. You'd probably want to use kCGBlendModeNormal as the blend mode in most cases.

like image 153
user1118321 Avatar answered Oct 17 '22 06:10

user1118321


I had created a function which gets array of images and will return single image. My code is below:

-(UIImage *)blendImages:(NSMutableArray *)array{

    UIImage *img=[array objectAtIndex:0];
    CGSize size = img.size;
    UIGraphicsBeginImageContext(size);

     for (int i=0; i<array.count; i++) {
        UIImage* uiimage = [array objectAtIndex:i];
        [uiimage drawAtPoint:CGPointZero blendMode:kCGBlendModeNormal alpha:1.0];
     }
     return UIGraphicsGetImageFromCurrentImageContext();
}

Hope this will help others too.

like image 31
Ankita Shah Avatar answered Oct 17 '22 06:10

Ankita Shah


You should composite your images into one -- especially because they have alpha channels.

To do this, you could

  • use UIGraphicsBeginImageContextWithOptions to create the image at the destination size (scale now, rather than when drawing to the screen and choose the appropriate opacity)
  • Render your images to the context using CGContextDrawImage
  • then call UIGraphicsGetImageFromCurrentImageContext to get the result as a UIImage, which you set as the image of the image view.
like image 1
justin Avatar answered Oct 17 '22 04:10

justin