In my application, I want to take the following steps:
1 - Screen capture, this part is not a problem for me, I use the following code:
- (UIImage *)captureScreen { UIGraphicsBeginImageContextWithOptions(self.view.frame.size, YES, 0.0f); [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; }
2 - I cropped the image using this function
- (UIImage *)cropImage(UIImage *)image inRect:(CGRect)rect { CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, rect); UIImage *resultImage = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); return resultImage; }
3 - Then I mask the cropped image with a black and white mask
- (UIImage *)maskImage:(UIImage *)image withMask:(UIImage *)maskImage { CGImageRef maskRef = maskImage.CGImage; CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef), CGImageGetHeight(maskRef), CGImageGetBitsPerComponent(maskRef), CGImageGetBitsPerPixel(maskRef), CGImageGetBytesPerRow(maskRef), CGImageGetDataProvider(maskRef), NULL, false); CGImageRef maskedRef = CGImageCreateWithMask([image CGImage], mask); UIImage *resultImage = [UIImage imageWithCGImage:maskedRef]; CGImageRelease(mask); CGImageRelease(maskedRef); return resultImage; }
However, the result obtained by me is that outside the form of the mask, the image is in black instead of transparent. Can anybody help me?
source share