【问题标题】:Issue with ALAssetsLibraryALAssetsLibrary 的问题
【发布时间】:2014-07-05 07:40:02
【问题描述】:

我正在尝试访问照片库中拍摄的最后一张照片,一切正常,除非照片库没有照片,我的应用程序崩溃了!如何找到停止崩溃的解决方案?这是我的代码:

-(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]];



                                 }else {

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


}];

}

【问题讨论】:

    标签: ios objective-c alassetslibrary


    【解决方案1】:

    您是否尝试将您的方法封装到if condition 中?

    -(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
            if ([group numberOfAssets] > 0) {
                [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]];
                    } else {
    
                    }
                }];
            }
        } failureBlock: ^(NSError *error) {
            // Typically you should handle an error more gracefully than this.
            NSLog(@"No groups");
        }];
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-18
      • 1970-01-01
      • 2011-09-14
      • 1970-01-01
      • 1970-01-01
      • 2015-01-05
      • 2014-08-02
      相关资源
      最近更新 更多