Hi all,
I'm looking for a Quartz 2D expert that might be able to see a flaw in the following code:
self.imageView.image = [self maskView:self.imageView withMask:[self createMaskForTool:self.toolView modifyForOutline:NO]];
- (UIImage *)maskView:(UIView *)view withMask:(UIImage *)maskImage
{
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef maskRef = maskImage.CGImage;
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
CGImageGetHeight(maskRef),
CGImageGetBitsPerComponent(maskRef),
CGImageGetBitsPerPixel(maskRef),
CGImageGetBytesPerRow(maskRef),
CGImageGetDataProvider(maskRef), NULL, false);
CGImageRef masked = CGImageCreateWithMask([viewImage CGImage], mask);
UIImage *resultingImage = [UIImage imageWithCGImage:masked];
CGImageRelease(mask);
CGImageRelease(masked);
return resultingImage;
}
- (UIImage *)createMaskForTool:(Tool *)tool modifyForOutline:(BOOL)forOutline
{
UIGraphicsBeginImageContext(self.imageView.frame.size);
// Draw black mask background
[[UIImage imageNamed:@"mask.png"] drawInRect:CGRectMake(0, 0, self.imageView.frame.size.width, self.imageView.frame.size.height)];
// Draw white mask shape
CGSize aspectRatioCorrectToolSize = [self aspectRatioCorrectSizeForImage:tool.maskImage toFitInSize:tool.frame.size];
[self.toolView.maskImage drawInRect:CGRectMake((tool.frame.origin.x+(tool.frame.size.width-aspectRatioCorrectToolSize.width)/2)-self.imageView.frame.origin.x+(forOutline ? OUTLINE_THICKNESS : 0),
(tool.frame.origin.y+(tool.frame.size.height-aspectRatioCorrectToolSize.height)/2)-self.imageView.frame.origin.y+(forOutline ? OUTLINE_THICKNESS : 0),
aspectRatioCorrectToolSize.width-(forOutline ? OUTLINE_THICKNESS : 0)*2,
aspectRatioCorrectToolSize.height-(forOutline ? OUTLINE_THICKNESS : 0)*2)];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultingImage;
}
These methods progressively mask an image. The app starts with a complete image and the user can use tools to "chop holes" through the image. To create the hole effect, I first create a mask with createMaskForTool:modifyForOutline: then use the resulting mask in maskView:withMask: to apply the mask to the image. The new masked image then replaces the existing imageView.image, and the new image is used as the base image the next time a new hole is to be carved. We've been weeding through the code to find other leaks that could cause the memory warnings that we're receiving, but invariably once you call these masking features 6 or 7 times in a row, the app will crash.