带有GCD的图像加载接收内存警告 [英] Image loading with GCD receiving memory warning
问题描述
我正在使用AssetsLibrary开发照片库应用程序以加载设备照片.在另一个VC中显示随机图像时,我注意到以下几点:我的完整分辨率图像在imageView上加载大约需要1或2秒的时间(比本地的photosApp长得多),并且我也从日志收到加载一些图像后出现内存警告".如果我将表示形式设置为fullScreenImage,则警告会停止,但我不希望这样做.要在视图上获得平滑的性能和高质量的图像,必须更改什么?
I'm developing a photo gallery application using AssetsLibrary to load my device photos. When presenting a random image in another VC I've noticed the following : it takes about 1 or 2 seconds for my full res image to load on the imageView (way much longer than the native photosApp) and I also get from the log "Received memory warning" after loading a few images. If I set my representation to fullScreenImage the warnings stop but I don't want this. What must I change for a smooth performance and high quality images on the view ?
这是代码,希望您能告诉我是什么问题:
Here's the code,hope you can tell me what's the problem :
这是我要在屏幕上显示图像的VC
This is the VC where I want to present my image on the screen
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"%@",assetsController);
detailImageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
[self.view addSubview:detailImageView];
detailImageView.image = smallImage; //small image is my asset thumbnail and is passed as an argument in my init function
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
ALAsset *asset = [assetsController.albumPics objectAtIndex:assetsController.index];
ALAssetRepresentation *representation = [asset defaultRepresentation];
bigImage = [[UIImage imageWithCGImage:[representation fullResolutionImage]]retain];
dispatch_async(dispatch_get_main_queue(), ^{
detailImageView.image = bigImage;
});
[pool release];
});
}
更新1
{
UIImageView *detailImageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
[self.view addSubview:detailImageView];
detailImageView.image = smallImage;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
ALAsset *asset = [assetsController.albumPics objectAtIndex:assetsController.index];
ALAssetRepresentation *representation = [asset defaultRepresentation];
UIImage *bigImage = [UIImage imageWithCGImage:[representation fullResolutionImage]];
dispatch_async(dispatch_get_main_queue(), ^{
detailImageView.image = bigImage;
});
[pool release];
});
}
推荐答案
bigImage
是实例变量吗?除了这里以外,是否在其他地方使用过?如果未在其他任何地方使用它,那么它应该是局部变量,并且您不应该保留它.如果要保留的是实例变量,则需要先释放先前的值,然后再为其分配新值.
Is bigImage
an instance variable? Is it used in any place other than here? If it is not used anywhere else, then it should be a local variable, and you shouldn't retain it. If it is an instance variable that you retain, you need to release the previous value before assigning a new value to it.
相同的讨论适用于detailImageView
这篇关于带有GCD的图像加载接收内存警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!