我正在寻找可能能够发现以下代码中的缺陷的 Quartz 2D 专家:
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;
}
这些方法逐渐掩盖图像。该应用程序从一个完整的图像开始,用户可以使用工具在图像上“挖洞”。要创建孔效果,我首先使用 createMaskForTool:modifyForOutline: 创建蒙版,然后在 maskView:withMask: 中使用生成的蒙版将蒙版应用于图像。然后新的蒙版图像替换现有的 imageView.image,并且在下次要雕刻新孔时将新图像用作基础图像。我们一直在清理代码以查找可能导致我们收到的内存警告的其他泄漏,但是一旦您连续调用这些屏蔽功能 6 或 7 次,应用程序就会崩溃。