我尝试修改 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];
}
我的代码(路径是断言库 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),这会使我的代码中的程序崩溃,但在使用原始代码时不会。
任何想法为什么?
PS 我使用 ARC,并为 FGallery 进行了自动转换。它适用于本地应用程序图像,但如前所述,我无法使其适用于相机胶卷图像。
编辑1:程序崩溃