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

发布于 2024-09-25 00:16:47 字数 2720 浏览 2 评论 0原文

我正在寻找一位 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 次,应用程序就会崩溃。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风吹短裙飘 2024-10-02 00:16:47

这似乎是很多额外的工作。为什么不直接设置layermask属性呢?

(我知道这并没有具体回答您的问题,但我认为研究一下可能会有所帮助)

This seems like a lot of extra work. Why not just set the layer's mask property?

(I'm aware this doesn't specifically answer your question, but I think it might be beneficial to look into)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文