7

I have two UIImageView on the UIView. Each UIImageView have one UIImage. Now i want create an one UIImage from two UIImageView.

Please give the suggestion how can i do this.

1
  • i'm not sure about what you are actually trying to implement. Is the resulting image is superimposed one of the 2 images or some other one.. Commented Jan 5, 2010 at 10:49

1 Answer 1

11

If your have a view with three UIImageViews and create corresponding outlets

IBOutlet UIImageView *imageView1;
IBOutlet UIImageView *imageView2;
IBOutlet UIImageView *imageView3;

You can do something like the following to combine imageViews 1 and 2 into imageView 3

UIGraphicsBeginImageContext(imageView1.image.size);  

CGRect rect = CGRectMake(0, 0, imageView1.image.size.width, imageView1.image.size.height);

[imageView1.image drawInRect:rect];  
[imageView2.image drawInRect:rect blendMode:kCGBlendModeScreen alpha:0.5];  

UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();  
UIGraphicsEndImageContext();  

[imageView3 setImage:resultingImage];

CGBlendMode allows you to choose from any of the following compositing operations for your images:

enum CGBlendMode {
   kCGBlendModeNormal,
   kCGBlendModeMultiply,
   kCGBlendModeScreen,
   kCGBlendModeOverlay,
   kCGBlendModeDarken,
   kCGBlendModeLighten,
   kCGBlendModeColorDodge,
   kCGBlendModeColorBurn,
   kCGBlendModeSoftLight,
   kCGBlendModeHardLight,
   kCGBlendModeDifference,
   kCGBlendModeExclusion,
   kCGBlendModeHue,
   kCGBlendModeSaturation,
   kCGBlendModeColor,
   kCGBlendModeLuminosity,
   kCGBlendModeClear,
   kCGBlendModeCopy,
   kCGBlendModeSourceIn,
   kCGBlendModeSourceOut,
   kCGBlendModeSourceAtop,
   kCGBlendModeDestinationOver,
   kCGBlendModeDestinationIn,
   kCGBlendModeDestinationOut,
   kCGBlendModeDestinationAtop,
   kCGBlendModeXOR,
   kCGBlendModePlusDarker,
   kCGBlendModePlusLighter
};
Sign up to request clarification or add additional context in comments.

1 Comment

Niels Castle- I need to perform same operation. My main imageview is static but my second imageview is draggable. So above code creates my second imageview same as main imageview bounds. How can I merge these two images into one without making its bound same as mainimageview? Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.