0

I have one UIImage *backgroundImage = [[UIImage alloc] initWithData:imgData];

I will draw an image on top of this image with:

CGSize finalSize = [backgroundImage size];
UIImage *newImage; UIGraphicsBeginImageContext(finalSize);
newImage = backgroundImage;
[newImage drawInRect:CGRectMake(0,0,finalSize.width,finalSize.height)];
UIImage *imageItem = [[UIImage alloc] initWithData:itemImgData];
[imageItem drawInRect:CGRectMake(10,10,100,100)];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Is there any way I can have reference to imageItem that is drawn on top of backgroundImage so that I can set it's visibility, either visible/invisible after the imageItem is drawn? Let's say the reference to imageItem is imageItemRef, I want to set it's visibility based on certain condition:

if(shouldVisible){
 [imageItemRef setHidden:TRUE];
else
 [imageItemRef setHidden:FALSE];

The reason I am not using another view to represent item that's on top of the backgroundImage is because i am implementing zooming on the backgroundImage and I want item to be zoomed as well when backgroundImage is zoomed.

Sasa
  • 205
  • 1
  • 3
  • 16

1 Answers1

0

You can set the alpha to be used for drawing operations in the context using:

CGContextSetAlpha(UIGraphicsGetCurrentContext(), 0.5);
Wain
  • 118,658
  • 15
  • 128
  • 151
  • If I have finished drawing, how do i do it? – Sasa Jun 26 '13 at 08:42
  • Add that line before `[imageItem drawInRect:...` and the image will draw with reduced alpha. – Wain Jun 26 '13 at 08:48
  • Your suggestion is for during drawing time , not after. I have edited my question to make it clearer of what i want. Hope it's clearer. Is it possible to do like that? – Sasa Jun 28 '13 at 00:59
  • No, you can't selectively change half of an image like that. You'd need to redraw or show the images as 2 different parts. – Wain Jun 28 '13 at 06:40
  • Put your drawing code in a method, add a parameter for the alpha, call the method to redraw the images whenever you need to change the alpha. Or, add the other image as a subview and change it's alpha. – Wain Jun 28 '13 at 08:22
  • I tried to use UIImageView. imageItem will have it's own UIImageView call imageItemView. When i add imageItemView as subview of backgroundImageView, the location of imageItemView is not the same as the location of drawn imageItem. Of course i checked the origin, it is the same. Why is that? – Sasa Jun 28 '13 at 08:23