Quartz 2D Experts - 屏蔽方法在连续6或7次调用后崩溃程序

时间:2010-09-28 22:19:04

标签: iphone quartz-2d masking

我正在寻找一位可能能够在以下代码中看到缺陷的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次调用这些屏蔽功能,应用程序就会崩溃。

1 个答案:

答案 0 :(得分:0)

这似乎是很多额外的工作。为什么不设置layer的{​​{1}}属性?

(我知道这并没有具体回答你的问题,但我认为这可能是有益的)