我正在尝试从UIImageView加载并显示照片库(相机胶卷)中的最后一张照片,一切正常!除了一件事!如果库中没有可用的图像,则应用程序崩溃!这是我的代码:

 -(void)importLastImage {

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

    // Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.
    [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

        // Within the group enumeration block, filter to enumerate just photos.
        [group setAssetsFilter:[ALAssetsFilter allPhotos]];

        // Chooses the photo at the last index
        [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:([group numberOfAssets]-1)]
                                options:0
                             usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {

                                 // The end of the enumeration is signaled by asset == nil.
                                 if (alAsset) {
                                     ALAssetRepresentation *representation = [alAsset defaultRepresentation];
                                     latestPhoto = [UIImage imageWithCGImage:[representation fullResolutionImage]];

                                    _lastImage.image = latestPhoto;

                                 }else {

                           //no image found !

                                 }
       }];
    }
                         failureBlock: ^(NSError *error) {
                             // Typically you should handle an error more gracefully than this.
                             NSLog(@"No groups");

                             UIAlertView *alert  = [[UIAlertView alloc]initWithTitle:@"ERROR" message:@"No Image found" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
                             [alert show];

}];

}

最佳答案

我决定在此处发布一个代码片段,您可以将其复制粘贴,其中包含我上面的评论中的建议。
如果我的英语不太清楚(我不是英语为母语的人),请提前抱歉。

您的崩溃是由于该行代码
[group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:([group numberOfAssets]-1)]

正如您所说,问题是,当库中没有可用图像时,numberOfAssets返回0,
并且由于您是使用numberOfAssets - 1创建索引的,因此您基本上是在尝试创建一个负索引,这会使程序崩溃。

我只是添加了一个if语句,以检查'if'numberOfAssets(表示它不是0),
只有这样,才执行以下枚举,因此,请避免出现任何负索引的情况。

无论如何,您可以在这里结伴:

-(void)importLastImage {

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

    // Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.
    [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

        // Within the group enumeration block, filter to enumerate just photos.
        [group setAssetsFilter:[ALAssetsFilter allPhotos]];

        if([group numberOfAssets]) {
            // Chooses the photo at the last index
            [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:([group numberOfAssets]-1)]
                                    options:0
                                 usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {

                                     // The end of the enumeration is signaled by asset == nil.
                                     if (alAsset) {
                                         ALAssetRepresentation *representation = [alAsset defaultRepresentation];
                                         latestPhoto = [UIImage imageWithCGImage:[representation fullResolutionImage]];

                                        _lastImage.image = latestPhoto;

                                     }else {

                               //no image found !

                                     }
           }];
        } else {
            NSLog(@"No images in photo library");
        }
    }
                         failureBlock: ^(NSError *error) {
                             // Typically you should handle an error more gracefully than this.
                             NSLog(@"No groups");

                             UIAlertView *alert  = [[UIAlertView alloc]initWithTitle:@"ERROR" message:@"No Image found" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
                             [alert show];

    }];

}

08-05 22:41