I want to mask an image by another image using CGLayerRef in off-screen rendering since it's suitable for high-quality rendering as it's mentioned in documentation
Core Graphics Layer Drawing : CGLayer objects allow your application to use layers for drawing. Layers are suited for the following: High-quality offscreen rendering of drawing that you plan to reuse.
But I couldn't find any example in objective-c that I could understand how it works. On my context, I want to mask an image with a shape image (solid color shape) I used to do it this way
//The context I use to mask my image with the mask image.
CGColorSpaceRef colorSpace= CGColorSpaceCreateDeviceRGB();
CGContextRef mainViewContentContext = CGBitmapContextCreate (NULL, _bigImageRect.size.width, _bigImageRect.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colorSpace);
//Mask image
CGContextClipToMask(mainViewContentContext,
CGRectMake(0,
-_bigImageRect.origin.y,
_bigImageRect.size.width,
_bigImageRect.size.height),
_maskImage.CGImage);
//Drawing the image on the mask image.
CGContextDrawImage(mainViewContentContext,
CGRectMake(0,
0,
_bigImageRect.size.width,
_bigImageRect.size.height),
_ImageToBeMasked.CGImage);
CGImageRef mainViewContentBitmapContext = CGBitmapContextCreateImage(mainViewContentContext);
CGContextRelease(mainViewContentContext);
UIImage*maskedImage = [UIImage imageWithCGImage:mainViewContentBitmapContext];
CGImageRelease(mainViewContentBitmapContext)
return maskedImage;
But how can I mask using CGLayers as below? I appreciate any help.
CGLayerRef layer = CGLayerCreateWithContext(mainViewContentContext, _bigImageRect.size, NULL);
CGContextDrawLayerInRect(mainViewContentContext, _bigImageRect, layer);
CGLayerRelease(layer);
//And then.. how can I do the masking using this CGLayerRef???