- (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];
}
变量 selectedImage 已在接口文件中声明为保留属性。正如你可以猜到的,我将一个图像缩略图存储在 selectedImage 中。然后我在另一个函数中重用它并在 dealloc 函数中释放它。
Instruments 显示该对象确实被释放但内存不断增加?这是否意味着释放对象不一定释放内存?
我经常用 UIImages 面对这种事情?有什么猜测吗?
我已将 [selectedImage release] 添加到确实被调用的 (void)dealloc 函数中,保留计数变为零并且对象确实被释放。但是分配了一些内存(我相信在使用 UIGraphicsGetImageFromCurrentImageContext(); 我猜)这没有被释放。我添加了 4-5 个图像,内存在模拟器中达到了惊人的 117 MB,然后在模拟器中回落到 48 MB。但是该应用程序在 iPhone 上崩溃了。创建图像时我应该采取其他方法吗?