I have a CALayer (imageLayer), and a subclassed CALayer that acts as a mask on the CALayer (maskLayer).
I am painting with a finger onto the maskLayer so that I can temporarily erase out part of the imageLayer.
I am accomplishing this by keeping one Bitmap Context (maskContext), and adding a stroke to it in touchesMoved. So my maskContext just holds all the strokes I have painted. In touchesMoved, I do [maskLayer setNeedsDisplay]
;
in my subclassed maskLayer, I have the drawInContext
method:
-(void)drawInContext:(CGContextRef)ctx
{
CGImageRef image = CGBitmapContextCreateImage(maskContext);
CGContextDrawImage(ctx, CGRectMake(0,0,self.frame.size.width, self.frame.size.height), image);
}
This all works out really well, but is very slow as it's drawing the whole view for each touchesMoved event.
I thought of using [maskLayer setNeedsDisplayInRect:rect]
but I wondered what the point is, as drawInContext
does not look at the rect. I could pass the rect to drawInContext
, but I could do that with just setNeedsDisplay
.
Is there something that setNeedsDisplayInRect
does that setNeedsDisplay
doesn't for a CALayer?
(I am aware that if I were using drawRect
, that would accept the defined rect, but a CALayer's drawInContext
doesn't)
Also, is there any better way of accomplishing this masking by painting with a finger? The main problem is that the CGContext is not a canvas and does not remember previous strokes, so they have to be开发者_StackOverflow redrawn all the time.
If you mark a layer to display by calling setNeedsDisplayInRect:
then you can use CGContextGetClipBoundingBox
to get the area marked to display in your drawInContext:
.
精彩评论