有没有人遇到过这个问题?任何建议都将不胜感激。
代码如下:有时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;
}发布于 2017-03-21 20:57:26
这个问题发生在从“我的照片流”中选择照片时。最后,我找到了解决方法来解决这个问题。我希望它能对你有所帮助。
-(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;
}https://stackoverflow.com/questions/42848260
复制相似问题