iPhone - Multiple CGBitmapContextCreateImage Calls

2019-04-16 18:14发布

Has anyone else come across this problem? ObjectAlloc climbs as a result of the CGBitmapContextCreateImage. Does Apple's software not fully releasing the objectalloc?

I am resizing images 12 times a second with a NSTimer. During resizing of the image I am also adding a photoshop like Gaussian blur effect by including interpolationQuality.

After using Instruments it does not show any memory leaks but my objectalloc just continues to climb. It points directly to CGBitmapContextCreateImage.
CGBitmapContextCreateImage > create_ bitmap_ data_provide > malloc

Anyone know of a solution? or Even possible ideas?

The Call within the NSTimer

NSString * fileLocation = [[NSBundle mainBundle] pathForResource:imgMain ofType:@"jpg"];
NSData * imageData = [NSData dataWithContentsOfFile:fileLocation];
UIImage * blurMe = [UIImage imageWithData:imageData];

CGRect rect = CGRectMake(0, 0, round(blurMe.size.width /dblBlurLevel), round(blurMe.size.width /dblBlurLevel)); 
UIImage * imageShrink = [self resizedImage: blurMe : rect : 3.0];   

CGRect rect2 = CGRectMake(0, 0, blurMe.size.width , blurMe.size.width ); 
UIImage * imageReize = [self resizedImage: imageShrink : rect2 : 3.0];

imgView.image = imageReize;

The Resize Function

-(UIImage *) resizedImage:(UIImage *)inImage : (CGRect)thumbRect : (double)interpolationQuality
{
    CGImageRef                  imageRef = [inImage CGImage];
    CGImageAlphaInfo    alphaInfo = CGImageGetAlphaInfo(imageRef);

    if (alphaInfo == kCGImageAlphaNone)
        alphaInfo = kCGImageAlphaNoneSkipLast;

    // Build a bitmap context that's the size of the thumbRect
    CGContextRef bitmap = CGBitmapContextCreate(
                                NULL,
                                thumbRect.size.width,
                                thumbRect.size.height,          
                                CGImageGetBitsPerComponent(imageRef),
                                4 * thumbRect.size.width,       
                                CGImageGetColorSpace(imageRef),
                                alphaInfo
                                );

    // Draw into the context, this scales the image
    CGContextSetInterpolationQuality(bitmap, interpolationQuality);
    CGContextDrawImage(bitmap, thumbRect, imageRef);

    // Get an image from the context and a UIImage
    CGImageRef  ref = CGBitmapContextCreateImage(bitmap);
    UIImage*    result = [UIImage imageWithCGImage:ref];

    CGContextRelease(bitmap);   // ok if NULL
    CGImageRelease(ref);

    return [result autorelease];
}

2条回答
够拽才男人
2楼-- · 2019-04-16 18:29

I compiled and ran your code as you have it and I don't see any leaks nor does my object alloc keep climbing. I run the code a couple of times a second and don't see any object growth in Instruments. I am only running on the simulator. I also tried a kCGInterpolationNone instead of 3.0 in case that is the problem, and still no leak.

Not sure why I don't get them and you do. You might want to just do this in the method:

-(UIImage *) resizedImage:(UIImage *)inImage : (CGRect)thumbRect : (double)interpolationQuality
{
    CGImageRef                  imageRef = [inImage CGImage];
    CGImageAlphaInfo    alphaInfo = CGImageGetAlphaInfo(imageRef);

    return inImage;
...

In order to make this method be meaningless and then watch to see if the object alloc continues to grow. If so, then the problem is elsewhere.

查看更多
Ridiculous、
3楼-- · 2019-04-16 18:38

That code overreleases result.

That said, it's likely that the issue is that the UIImage is not getting deallocated, and the UIImage is holding onto the CGImage, and the CGImage is holding onto the memory that was allocated under CGBitmapContextCreate.

Use instruments to see if UIImages are not getting deallocated, and if so try to debug why.

查看更多
登录 后发表回答