views:

859

answers:

1

I have done research and tried several times to release the UIImage memory and have been unsuccessful. I saw one other post on the internet where someone else was having this same issue. Everytime imageScaledToSize is called, the ObjectAlloc continues to climb.

In the following code I am pulling a local image from my resource directory and resizing it with some blur. Can someone provide some help on how to release the memory of the UIImages called....scaledImage and labelImage. This is the chunk of code where the iPhone Intruments has shown to have the ObjectAlloc build up. This chunk of code is called several times with an NSTimer.

//Get local image from inside resource
NSString * fileLocation = [[NSBundle mainBundle] pathForResource:imgMain ofType:@"jpg"];
    NSData * imageData = [NSData dataWithContentsOfFile:fileLocation];
    UIImage * blurMe = [UIImage imageWithData:imageData];

//Resize and blur image
    UIImage * scaledImage = [blurMe _imageScaledToSize:CGSizeMake(blurMe.size.width / dblBlurLevel, blurMe.size.width / dblBlurLevel) interpolationQuality:3.0];
    UIImage * labelImage = [scaledImage _imageScaledToSize:blurMe.size interpolationQuality:3.0];
    imgView.image = labelImage;
A: 

You can wrap the calls in an NSAutoreleasePool, where their results will be pooled. Then you can call [pool drain] on that pool and its contents will be released, including the images.

Note however that you will not be able to use the images outside of the NSAutoreleasePool's scope, so you code might want to look something like:

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

UIImage * scaledImage = [blurMe _imageScaledToSize:CGSizeMake(blurMe.size.width / dblBlurLevel, blurMe.size.width / dblBlurLevel) interpolationQuality:3.0];
UIImage * labelImage = [scaledImage _imageScaledToSize:blurMe.size interpolationQuality:3.0];
UIImage * imageCopy = [[UIImage alloc] initWithCGImage:labelImage.CGImage]; // Gives a non-autoreleased copy of labelImage

[pool drain]; // deallocates scaledImage and labelImage

imgView.image = imageCopy; // retains imageCopy

UPDATE:

If the above is still giving you problems, please see the solution I posted to this question. The question involves rotating an image 90 degrees instead of scaling it, but the premise is the same (it's just the matrix transformation that is different). Using code like in the answer I posted should give you greater control over your memory management and steer you away from using undocumented APIs like _imageScaledToSize.

fbrereto
Would you happen to have an small example of it? I have never worked with NSAutoreleasePool before
bbullis21
This maybe a dumb question, but if I am not able to use the images outside of the NSAutorelease Pool how would I be able to use imageCopy in your example?
bbullis21
(I updated the example to try and be a little more clear.) `imageCopy` is not placed into the autorelease pool and will require your code to memory manage, which is what you want because you are using it in `imgView` which will last longer than the autorelease pool. The images that are in the autorelease pool -- labelImage and scaledImage -- you are not referencing after the pool has drained, so you're ok.
fbrereto
Thank You, example worked!
bbullis21
Sorry, After doing further testing this release pool did not work, the objectalloc is still climbing on the UIImage imageScaledToSize call
bbullis21
Are you properly releasing imgView.image? After the sample code above runs its retainCount will be 2, so releasing it once will not be enough and could account for the leak.
fbrereto
Is there any solution to this. I am facing the same issue.
rkb