0

我正在尝试从 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。提前致谢。

4

1 回答 1

1

几个小时后,我发现这里的代码没有问题。这是提取相机胶卷中最后一张照片的正确代码(如果有人需要它)。问题在 doTheJobWithImage: 里面,我解决了它替换

[self doTheJobWithImage:image];

并将生成的图像存储在其他地方,然后在完成图片枚举后执行 doTheJobWithImage: 。这样说以防万一其他人感兴趣。

于 2013-02-17T21:35:19.813 回答