Hi Everyone,
I have searched this forum several times now, and i do not seem to find an answer to my problem.
Other people have asked the same question already but i'm under the impression the problem is not as easy to solve.
Basically what i am trying to do is the following,
I would like to put a layer on top of a picture, and then erase that layer through touches or accelerometer movement, 'scratching away' the place where a touch has occured.
Like this:
Now i have got this working like this:
Code:
- (void)drawRect:(CGRect)rect {
// Retrieve the graphics context
CGContextRef context = UIGraphicsGetCurrentContext();
//Get the drawing image
CGImageRef maskImage = [self drawImageWithContext:context inRect:rect];
// Get the mask from the image
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskImage)
, CGImageGetHeight(maskImage)
, CGImageGetBitsPerComponent(maskImage)
, CGImageGetBitsPerPixel(maskImage)
, CGImageGetBytesPerRow(maskImage)
, CGImageGetDataProvider(maskImage)
, NULL
, false);
//make sure the images are not upside down
CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
//Draw the base picture
CGContextDrawLayerInRect(context, rect, firstLayer);
//Add clipping
CGContextClipToMask(context, rect, mask);
//Release the mask
CGImageRelease(mask);
//Release the maskImage
CGImageRelease(maskImage);
//draw the second layer
CGContextDrawLayerInRect(context, rect, secondLayer);
}
As you can see i have a drawrect method, where i make an image based on the touches. This image is being used to get an imageMask.
I draw the first picture, lay down the mask, and then draw the second picture.
This works effectively resolving my problem.
But there are two drawbacks:
1) If i draw another thing over my last picture, this is also clipped...
2) Perhaps the most important thing, when i call my drawrect several times (from accelerometer), i lose performance, making the application impossible to use...
So basically my question is,
is there another way on resolving my problem? Do i need to look at this another way? Do i need to copy paste pixels?
Do i need alpha blending? Or do i need to look at OpenGL?
I'm hoping somebody can finally shed some light on this, because i'm really sitting in the dark here...