views:

206

answers:

2

Hi, I'm working on making an iPhone App where there are two ImageViews and when you touch the top one, wherever you tapped, the bottom one shows instead.

Basically what I want to do is cut an ellipse/roundedrect out of an image. To do this I was thinking on either clipping the image, or changing the alpha pixels in the rect to zero. I am new to Quartz 2D Programming so I am not sure how to do this.

Assuming I have:

UIImageView *topImage;
UIImageView *bottomImage;

How do I delete a CGRect/Ellipse/RoundedRect from these images. This is kind of like those lottery tickets that you have to scratch off to reveal if you won.

A: 

I would generally try to make a mask from a path (here containing a rounded rectangle), then masking the image with it, as demonstrated in the apple docs. The one of the benefits of this is that for hit testing all you need to do is CGPathContainsPoint with the point that was touched (as in it will test whether it was in the visible area of the image).

Jared P
So what whould I put in the drawrect method for this?I'm not experienced with Quartz
gizmoitai
so the section of the docs concerning the actual clipping is at the bottom (sorry I forgot to mention that), but basically create a path, draw a rounded rectangle where you want it, clip the context to the path, draw the image, (unclip the path?), and continue on. I'm not including specific code because its unnecessary, just dig around the apple docs for the function calls. google also. Heres something for the rounded rect pathhttp://www.drobnik.com/touch/2010/02/drawing-rounded-rectangles/
Jared P
I tried this code: CGContextRef ctx = UIGraphicsGetCurrentContext(); CGRect frame = CGRectMake(100, 100, 100, 100); CGPathRef roundedRectPath = [self newPathForRoundedRect:frame radius:5]; CGContextAddPath(ctx, roundedRectPath); CGContextClip (ctx); CGPathRelease(roundedRectPath);(Together with the rounded rect path function you sent)This is on a white view and beneath the view there is a gray Window, so I thought this would just show gray instead of white in CGRect frame but it didn't do anything...
gizmoitai
A: 

I tried this code:

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect frame = CGRectMake(100, 100, 100, 100);
CGPathRef roundedRectPath = [self newPathForRoundedRect:frame radius:5];
CGContextAddPath(ctx, roundedRectPath);
CGContextClip (ctx);
CGPathRelease(roundedRectPath);

(Together with the rounded rect path function you sent) This is on a white view and beneath the view there is a gray Window, so I thought this would just show gray instead of white in CGRect frame but it didn't do anything...

gizmoitai
you need to put what you actually want to draw between the context clip and the path release (eg draw the image, probably in 'frame')
Jared P
Ok, I tried adding this before releasing the path:[bottomImage.image drawInRect:frame];All it did was draw the image again in frame, obviously... That's not what I want to do. I want to cut out frame from within the image. So that there is a transparent space in the middle of the image.
gizmoitai