我使用的是 ELCImagePicker 并且在使用 asselts 从照片库一次导入多张照片时遇到了同样的问题。我们无法减少导入时间,但崩溃问题将得到解决。
for (int j=0; j<[assetArray count]; j++)
{
@autoreleasepool // This is compiler level feature so will only work on xcode 4.1 or above
{
ALAsset *assest = [assetArray objectAtIndex:j];
CGImageRef imageRef = assest.defaultRepresentation.fullResolutionImage;
UIImage *image = [UIImage imageWithCGImage:imageRef];
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:documentsPath atomically:YES];
}
}
如果可能,请尝试仅将 AssetURL 存储在assetArray 中,而不是整个 ALAsset 对象,并从 url 一次创建 ALAsset,这样可能有助于减少内存消耗。在这种情况下,您将需要使用块作为
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
CGImageRef iref = [[myasset defaultRepresentation] fullResolutionImage];
if (iref) //You have image so use it
{
}
};
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror)
{
NSLog(@"Can't get image - %@",[myerror localizedDescription]);
};
ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
[assetslibrary assetForURL:imageURL resultBlock:resultblock failureBlock:failureblock];