Ankita Shah
Ankita Shah

Reputation: 2248

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

Upvotes: 5

Views: 1534

Answers (4)

Ankita Shah
Ankita Shah

Reputation: 2248

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.

Upvotes: 2

justin
justin

Reputation: 104698

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.

Upvotes: 1

lee
lee

Reputation: 8105

You can use:

typedef enum _imageType{
  image1,
  image2,
  ...
  imageN
}imageType;

and declare in @interface

imageType imgType;

in .h file.

And in the.m file

-(void)setImageType:(imageType)type{
  imgType = type;
}

and then you can use function setImageType: to set any images what you want.

Upvotes: 0

user1118321
user1118321

Reputation: 26345

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.

Upvotes: 2

Related Questions