fetch - 有时 PHAsset fetchAssetsWithLocalIdentifiers :options method return a empty fetch result

标签 fetch phasset

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

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

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

最佳答案

从“我的照片流”中选择照片时会出现此问题。最后,我得到了这个解决方法来解决它。希望对您有帮助。

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

关于fetch - 有时 PHAsset fetchAssetsWithLocalIdentifiers :options method return a empty fetch result,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42848260/

相关文章:

javascript - 如何 Hook 不使用服务 worker 的获取方法?

json - 将 json 文件中的静态数据导入 Backbone 模型

ios - PHAssetManager 在请求图像时忽略 targetSize?

javascript - 在 Nativescript 应用程序中将 PHAsset/AVAsset 转换为 mp4 视频

javascript - 以 html 形式获取帖子响应,但也包含状态

php - mysql取数组报错2

arrays - 从数组中删除重复的 PHAssets?

ios - 没有 PHAsset 是否可以将照片存储在 iPhone 上?

javascript - 如何防止 react-query 最初获取但启用重新获取?

iOS Photokit - PHAsset pixelWidth 和 pixelHeight 与高分辨率图像不匹配