Eleonora Ciceri
Eleonora Ciceri

Reputation: 1798

How to position an object over an image and store it

I am creating a "cartoonizer" application, which takes an image as an input and modifies it so as to format it in comics style.

At the moment, the original image is stored in a UIImageView, and I access to it by doing:

imageView.image

I was wondering whether it is possible to position an object on the image imageView.image (like a balloon) and then store the image with the object on it, like if it was originally part of the image content.

Thank you in advance.

Upvotes: 0

Views: 85

Answers (1)

Woodstock
Woodstock

Reputation: 22966

You can do it like this, assuming you have correct opacity in the top image.

Like you said you get your image from imageView.image:

From here: blend two uiimages based on alpha/transparency of top image

UIImage *bottomImage = [UIImage imageNamed:@"bottom.png"];
UIImage *image = [UIImage imageNamed:@"top.png"];

CGSize newSize = CGSizeMake(width, height);
UIGraphicsBeginImageContext( newSize );

// Use existing opacity as is
[bottomImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
// Apply supplied opacity
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:0.8];

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

More compositing techniques here: http://mobile.tutsplus.com/tutorials/iphone/ios-sdk-advanced-uiimage-techniques/

Upvotes: 1

Related Questions