简体   繁体   中英

iPhone Memory Management with UIImage

- (void)imagePickerController:(UIImagePickerController *)picker 
        didFinishPickingImage:(UIImage *)image 
                  editingInfo:(NSDictionary *)editingInfo {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    [self.pickerTrigger setImage:image];
    [self.button setTitle:@" " forState:UIControlStateNormal];
    [self.button setTitle:@" " forState:UIControlStateSelected];
    [self.button setTitle:@" " forState:UIControlStateHighlighted];

    CGSize oldSize = [image size];
    CGFloat width = oldSize.width;
    CGFloat height = oldSize.height;
    CGSize targetSize = CGSizeMake(320.0, 400.0);
    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);

    if (CGSizeEqualToSize(oldSize, targetSize) == NO)
    {
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;

        if (widthFactor > heightFactor)
            scaleFactor = widthFactor;
        else
            scaleFactor = heightFactor;
        scaledWidth = width * scaleFactor;
        scaledHeight = height * scaleFactor;

        if (widthFactor > heightFactor)
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
        else if (widthFactor < heightFactor)
            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
    }

    UIGraphicsBeginImageContext(targetSize);

    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width = scaledWidth;
    thumbnailRect.size.height = scaledHeight;
    [image drawInRect:thumbnailRect];

    self.selectedImage = UIGraphicsGetImageFromCurrentImageContext();

    NSLog([NSString stringWithFormat:@"After getting from DB %d", [selectedImage retainCount]]);
    UIGraphicsEndImageContext();
    [pool release];
    [picker dismissModalViewControllerAnimated:YES];
}

the variable selectedImage has been declared as a retained property in the interface file. As you can guess I thumbnail an image to store it in selectedImage. then I reuse it in another function and release it in the dealloc function.

Instruments shows that the object does get deallocated but memory keeps increasing? Does it mean that releasing objects doesn't necessarily free the memory?

I face this kinda thing too often with UIImages? Any guesses?

I have added [selectedImage release] to the (void)dealloc function which does get called , retain count becomes zero and the object does get deallocated. But some memory is allocated (I believe while using UIGraphicsGetImageFromCurrentImageContext(); I guess) which is not freed. I add 4-5 Images the memory reaches a whopping 117 MB in the simulator then it drops back to 48 MB in the simulator. But the app crashes on the iPhone. Should I take some other approach while creating the image??

If you have selectedImage declared as a "retained property" then you have to send it a message to release it.

This is because the image is retained inside the implicit call for the setter: self.selectedImage = XXX . You cannot see it, because its in the code the compiler creates for the property setter.

Instruments shows that the object does get deallocated but memory keeps increasing? Does it mean that releasing objects doesn't necessarily free the memory?

When you release an object it reduces it ref counter by 1, it is only released form memory when that counter hits zero.

Your UIImage will not be released if something else is using it, this might be your code or a view you created in Interface builder.

It is possible you are calling retain too main times thus the ref counter will never reach zero.

Also, remember that at the start of every event loop on the iPhone an Autorelease Pool is created. You can always add your object to that and have it cleaned up automatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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