0

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.

Eleanore
  • 1,750
  • 3
  • 16
  • 33

1 Answers1

1

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/

Community
  • 1
  • 1
Woodstock
  • 22,184
  • 15
  • 80
  • 118
  • And what if I want to maintain it clickable? Like if I want to user to move it around the image, resize it... – Eleanore Sep 11 '13 at 15:50
  • Then you need to subclass UIImageView and override the touchesbegan / moved / ended. If you want to maintain clickable then you are not looking to composit (embed the images together to create one new image) which is what I thought you were asking. – Woodstock Sep 11 '13 at 15:52
  • The embedding is the final step: when someone presses "save" so as to save the comics, you blend the images together (and thus, for this part, your solution is fine). However, it is up to the user where to place the balloon and how big it has to be. Thus, at least in the first interactions step, the top image should be clickable. – Eleanore Sep 11 '13 at 15:55
  • 1
    That's fine, in that case use subclassed UIImageViews to position the images, and when you decide to blend pull both the UIImageview images out, store them in two UIImages and use my solution. – Woodstock Sep 11 '13 at 15:57