我正在尝试从 iphone 相机胶卷中获取最后一张照片。我使用以下代码:
UIImage* __block image = [[UIImage alloc] init];
ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup* group, BOOL* stop) {
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
if ([group numberOfAssets] > 0) {
[group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:[group numberOfAssets]-1] options:NSEnumerationConcurrent usingBlock:^(ALAsset* alAsset, NSUInteger index, BOOL* innerStop) {
if (alAsset) {
ALAssetRepresentation* rawImage = [alAsset defaultRepresentation];
image = [UIImage imageWithCGImage:[rawImage fullScreenImage]];
[self doTheJobWithImage:image];
// Inside doTheJobWithImage: a segue is also performed at the end
}
}];
}
} failureBlock:^(NSError* error) {
NSLog(@"Error: %@", [error localizedDescription]);
}];
它可以工作,但有缺陷(我正在使用 Instruments and Zombies 来调试它):
- 它似乎阅读了相机胶卷中的每一张照片。那么,第一个问题:不枚举AssetsAtIndexes:只检索指定的图像(atIndexes)?我的代码有什么问题?
- 加上之前的问题,当相机胶卷中有很多照片时,内存就会成为问题。如果太多,或者如果它有效,我的应用程序崩溃,似乎检索到的图像永远不会被释放,所以我第二次或第三次调用此代码时,由于内存泄漏,它无论如何都会崩溃。所以第二个问题:如何在调用 doTheJobWithImage: 后强制我的代码释放所有内容?
我正在使用 xcode 4.6、ios 6 和 ARC。提前致谢。