首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >与ALAssetsLibrary的问题

与ALAssetsLibrary的问题
EN

Stack Overflow用户
提问于 2014-07-05 07:40:02
回答 1查看 247关注 0票数 0

我试图访问照片库上的最后一张照片,一切正常,除非,照片库没有照片我的应用程序崩溃!我怎样才能找到解决办法来停止崩溃呢?这是我的代码:

代码语言:javascript
复制
-(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");


}];

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-05 07:55:29

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

代码语言:javascript
复制
-(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");
    }];

}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24584175

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档