简体   繁体   English

cgimagemaskcreate 和 memory 泄漏,如何修复它

[英]cgimagemaskcreate and memory leak, how to fix it

i have this code:我有这个代码:

 Texture2D *cachedTexture;

if(cachedTexture = [cachedTextures objectForKey:aName]) {
    return cachedTexture;
}

// We are using imageWithContentsOfFile rather than imageNamed, as imageNamed caches the image in the device.
// This can lead to memory issue as we do not have direct control over when it would be released.  Not using
// imageNamed means that it is not cached by the OS and we have control over when it is released.
NSString *filename = [aName stringByDeletingPathExtension];
NSString *filetype = [aName pathExtension];
NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:filetype];

UIImage *maskImage=[UIImage imageWithContentsOfFile:path];
CGImageRef maskRef = maskImage.CGImage;     
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                    CGImageGetHeight(maskRef),
                                    CGImageGetBitsPerComponent(maskRef),
                                    CGImageGetBitsPerPixel(maskRef),
                                    CGImageGetBytesPerRow(maskRef),
                                    CGImageGetDataProvider(maskRef), NULL, false);  

cachedTexture = [[Texture2D alloc] initWithMask:mask filter:aFilter];

[cachedTextures setObject:cachedTexture forKey:aName];

static analyzer wrote this: 1)call to function CGImageMaskCreate returns a core foundation object with a +1 retain counter 2) object allocated and stored into mask is not referenced later in this execution path and has a retain counter of +1 [mask release] not work...mask is not a poiner... how can i fix this leak? static analyzer wrote this: 1)call to function CGImageMaskCreate returns a core foundation object with a +1 retain counter 2) object allocated and stored into mask is not referenced later in this execution path and has a retain counter of +1 [mask release]不工作......面具不是一个指针......我该如何解决这个泄漏?

Here is a non-leaking util method that does what you need:这是一个不泄漏的 util 方法,可以满足您的需要:

- (UIImage*) maskImage:(UIImage *)image
              withMask:(UIImage *)maskImage
{
        CGImageRef imageRef = image.CGImage;
        CGImageRef maskRef = maskImage.CGImage;

        CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                      CGImageGetHeight(maskRef),
                                      CGImageGetBitsPerComponent(maskRef),
                                      CGImageGetBitsPerPixel(maskRef),
                                      CGImageGetBytesPerRow(maskRef),
                                      CGImageGetDataProvider(maskRef),
                                      NULL, // decode should be NULL
                                      FALSE // shouldInterpolate
                                      );

        CGImageRef masked = CGImageCreateWithMask(imageRef, mask);
        CGImageRelease(mask);
        UIImage *maskedImage = [UIImage imageWithCGImage:masked];
        CGImageRelease(masked);
        return maskedImage;
}

Use CGImageRelease(mask) CGImageMaskCreate returns a CGImageRef with a retain count of 1.使用CGImageRelease(mask) CGImageMaskCreate 返回一个保留计数为 1 的 CGImageRef。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM