从相机胶卷读取UIImage时内存分配大(内存泄漏?)的原因 [英] Cause of big memory allocation (memory leak?) when reading UIImage from camera roll

查看:160
本文介绍了从相机胶卷读取UIImage时内存分配大(内存泄漏?)的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试修改FGallery(https://github.com/gdavis/FGallery-iPhone)。
我需要它来从相机胶卷读取图像,但我得到了内存泄漏。

I try to modify FGallery (https://github.com/gdavis/FGallery-iPhone). I need it to read images from the camera roll, but I get memory leak.

旧代码(路径是文件位置):

Old code (path is file location):

@autoreleasepool {

NSString *path = [NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle]   bundlePath],_thumbUrl];
_thumbnail = [UIImage imageWithContentsOfFile:path];
_hasThumbLoaded = YES;
_isThumbLoading = NO;
[self performSelectorOnMainThread:@selector(didLoadThumbnail) withObject:nil   waitUntilDone:YES];
}

我的代码(路径是断言库网址):

My code (path is assert library url):

@autoreleasepool {

ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset) {
   ALAssetRepresentation *rep = [myasset defaultRepresentation];
   CGImageRef iref = [rep fullResolutionImage];
   if (iref) {
       _thumbnail = [UIImage imageWithCGImage:iref];
       _hasThumbLoaded = YES;
       _isThumbLoading = NO;
       [self performSelectorOnMainThread:@selector(didLoadThumbnail) withObject:nil   waitUntilDone:YES];
   }
};        

ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror) {
   NSLog(@"booya, cant get image - %@",[myerror localizedDescription]);
};     

NSURL *asseturl = [NSURL URLWithString:_thumbUrl];
[assetslibrary assetForURL:asseturl 
resultBlock:resultblock
failureBlock:failureblock];
}
}

对于相同的图像,我获得了大量内存分配(-didReceiveMemoryWarning)在我的代码中使程序崩溃,但在使用原始代码时却没有。

For the same images, I get a big memory allocation (-didReceiveMemoryWarning) that crash the program in my code, but not while using the original code.

任何想法为什么?

PS我使用ARC,并为FGallery进行了自动转换。它适用于本地应用程序图像,但正如所说,我无法使其适用于相机胶卷图像。

P.S. I use ARC, and did the automatic transition for FGallery. It works fine for local app images, but as said, I can't make it to work for camera roll images.

编辑1:程序崩溃

推荐答案

我想我明白了。
ALAssetsLibraryAssetForURLResultBlock resultblock正在另一个线程上运行。
因此@autoreleasepool不适用于它。 (每个线程都有自己的自动释放池)。因此,由于许多自动释放分配(图像),内存占用率要高得多。
在块内添加@autoreleasepool停止了崩溃和大内存分配。

i think i got it. the "ALAssetsLibraryAssetForURLResultBlock resultblock" is running on a different thread. and so the "@autoreleasepool" does not apply to it. (each thread have it's own autorelease pool). hence, the memory footprint is much higher due to lot of "autoreleased" allocations (images). adding "@autoreleasepool" inside the block stopped the crashs and big memory allocations.

简而言之:

ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset) {
   @autoreleasepool {
      ALAssetRepresentation *rep = [myasset defaultRepresentation];
      CGImageRef iref = [rep fullResolutionImage];
      if (iref) {
          _thumbnail = [UIImage imageWithCGImage:iref];
          _hasThumbLoaded = YES;
          _isThumbLoading = NO;
          [self performSelectorOnMainThread:@selector(didLoadThumbnail) withObject:nil   waitUntilDone:YES];
       }
   }
}; 

感谢所有回复的人。

这篇关于从相机胶卷读取UIImage时内存分配大(内存泄漏?)的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆