开发者

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),
                    开发者_JAVA百科                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?


Here is a non-leaking util method that does what you need:

- (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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜