ami*_*mit 1 iphone xcode memory-leaks ios alassetslibrary
我尝试修改FGallery(https://github.com/gdavis/FGallery-iPhone).我需要它从相机胶卷读取图像,但我得到内存泄漏.
旧代码(路径是文件位置):
@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];
}
Run Code Online (Sandbox Code Playgroud)
我的代码(路径是断言库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];
}
}
Run Code Online (Sandbox Code Playgroud)
对于相同的图像,我得到一个大的内存分配(-didReceiveMemoryWarning),使我的代码中的程序崩溃,但不是在使用原始代码时.
有什么想法吗?
PS我使用ARC,并为FGallery做了自动转换.它适用于本地应用程序图像,但正如所说,我无法使其适用于相机胶卷图像.
编辑1:程序崩溃
我想我明白了."ALAssetsLibraryAssetForURLResultBlock resultblock"正在另一个线程上运行.因此"@autoreleasepool"不适用于它.(每个线程都有自己的自动释放池).因此,由于许多"自动释放"分配(图像),内存占用率要高得多.在块内添加"@autoreleasepool"可以阻止崩溃和大内存分配.
简而言之:
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];
}
}
};
Run Code Online (Sandbox Code Playgroud)
感谢所有回复的人.
| 归档时间: |
|
| 查看次数: |
4133 次 |
| 最近记录: |