Create new Image using existing image in IOS

Create new image in IOS using the existing images using the following code

    UIImage *bottomImage = [UIImage imageNamed:@"background.png"]; //background image
    UIImage *image       = [UIImage imageNamed:@"foreground.png"]; //foreground image
    CGSize newSize = CGSizeMake(bottomImage.size.width, bottomImage.size.height);
    UIGraphicsBeginImageContext( newSize );
    // Use existing opacity as is
    [bottomImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    // Apply supplied opacity if applicable
    [image drawInRect:CGRectMake(newSize.width-90,10,80,80) blendMode:kCGBlendModeNormal alpha:0.4];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

Here ‘bottomImage’ is the main Background image
‘image’ is the foreground image to be placed on the background image.
‘newSize’ is the size of the new image where the new image will have the size of background image

CGSize newSize = CGSizeMake(bottomImage.size.width, bottomImage.size.height);

Also here the foreground image is placed at the right top corner of the background image with a semi transparent overlay

[image drawInRect:CGRectMake(newSize.width-90,10,80,80) blendMode:kCGBlendModeNormal alpha:0.4];

After all ‘newImage’ will contains the newly created image.

Thanks.

Sharing is caring!

Leave a Comment