【问题标题】:Image loading with GCD receiving memory warning使用 GCD 加载图像时收到内存警告
【发布时间】:2013-03-12 09:49:11
【问题描述】:

我正在使用 AssetsLibrary 开发一个照片库应用程序来加载我的设备照片。在另一个 VC 中呈现随机图像时,我注意到以下情况:我的完整 res 图像加载到 imageView 大约需要 1 或 2 秒(比本机 photosApp 长得多),我还从日志中得到“已接收加载几张图片后出现内存警告”。如果我将我的表示设置为 fullScreenImage 警告会停止,但我不想要这个。为了在视图上获得流畅的性能和高质量的图像,我必须进行哪些更改?

这是代码,希望你能告诉我什么问题:

这是我想在屏幕上展示我的图像的 VC

- (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 release];之后 detailImageView.image = bigImage;
  • @bhuXan 已经尝试过了,但在加载一些图像后仍然收到警告...
  • 使用 jpeg 图像或将 png 转换为 jpeg 应该可以......
  • 这只是一种预感,但如果你 NSLog 是一项资产,你会得到什么?它看起来像 Core Data 对象吗?
  • @Tommy 我登录时的输出是:Photo_Share[1420:907] 请参阅资产:ALAsset - 类型:照片,URLs:assets-library://asset/asset.JPG?id =00000000-0000-0000-0000-000000000072&ext=JPG

标签: ios multithreading grand-central-dispatch alassetslibrary nsautoreleasepool


【解决方案1】:

bigImage 是实例变量吗?是不是在这里以外的地方使用过?如果它没有在其他任何地方使用,那么它应该是一个局部变量,你不应该保留它。如果是你保留的实例变量,需要先释放之前的值,再给它赋新值。

同样的讨论适用于detailImageView

【讨论】:

  • 确实,两者都是实例变量,但从未在其他地方使用过,所以我将它们转换为局部变量。看看更新
  • 另外,detailImageView 需要在方法结束时释放
  • 刚刚发布,结果一样;现在我会问:如果我将其设置为本地视图,每次加载视图时不会绘制 detailImageView 吗?
猜你喜欢
  • 2014-01-09
  • 2013-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-30
  • 1970-01-01
相关资源
最近更新 更多