首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有时,PHAsset fetchAssetsWithLocalIdentifier:options方法会返回空的获取结果

有时,PHAsset fetchAssetsWithLocalIdentifier:options方法会返回空的获取结果
EN

Stack Overflow用户
提问于 2017-03-17 10:37:33
回答 1查看 1.4K关注 0票数 2

有没有人遇到过这个问题?任何建议都将不胜感激。

代码如下:有时resultAsset为空。也许偶尔会在iOS9.3上发生。

代码语言:javascript
复制
- (PHAsset*)retrievePHAssetFromLocalIdentifier:(NSString*)localIdentifier {
    if (!localIdentifier) {
        return nil;
    }
    NSArray *localIdentifiers = @[localIdentifier];
    PHFetchResult *result = [PHAsset fetchAssetsWithLocalIdentifiers:localIdentifiers options:nil];
    PHAsset *resultAsset = [result firstObject]; //Sometimes the resultAsset is empty. Maybe happened on iOS9.3 occasionally.
    if (!resultAsset) {
        NSLog(@"can't retrieve PHAsset from localIdentifier:%@",localIdentifier);
    }
    return resultAsset;
}
EN

回答 1

Stack Overflow用户

发布于 2017-03-21 20:57:26

这个问题发生在从“我的照片流”中选择照片时。最后,我找到了解决方法来解决这个问题。我希望它能对你有所帮助。

代码语言:javascript
复制
-(PHAsset*)workAroundWhenAssetNotFound:(NSString*)localIdentifier{

    __block PHAsset *result = nil;
    PHFetchOptions *userAlbumsOptions = [PHFetchOptions new];
    userAlbumsOptions.predicate = [NSPredicate predicateWithFormat:@"estimatedAssetCount > 0"];
    PHFetchResult *userAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumMyPhotoStream options:userAlbumsOptions];

    if(!userAlbums || [userAlbums count] <= 0) {            
        return nil;
    }

    [userAlbums enumerateObjectsUsingBlock:^(PHAssetCollection *collection, NSUInteger idx1, BOOL *stop) {
        PHFetchOptions *fetchOptions = [PHFetchOptions new];
        fetchOptions.predicate = [NSPredicate predicateWithFormat:@"self.localIdentifier CONTAINS %@",localIdentifier];
        PHFetchResult *assetsFetchResult = [PHAsset fetchAssetsInAssetCollection:collection options:fetchOptions];

        [assetsFetchResult enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop1) {
            if(asset){
                result = asset;
                *stop1 = YES;
                *stop = YES;
            }
        }];
    }];
    return result;
}

参考链接:How to get photo from My Photo Stream Album

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

https://stackoverflow.com/questions/42848260

复制
相关文章

相似问题

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