检测照片图库为空



我正在尝试加载并显示照片库(相机胶卷)中的最后一张照片,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语句,以检查"如果"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];
    }];
}

最新更新