问题
I need to retrieve the names of all the PHAsset
existing in the Camera Roll, individually and in a short time.
To get the file name, I use the documented originalFilename
property of PHAssetResource
associated to the PHAsset
.
This works fine for the first assets, but at some point (after around 400 assets), it starts failing and returning nil
every time.
Here is a code that shows this behavior (running on an iPhone 7 with ~800 photos in the Camera Roll):
PHFetchResult *result = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum
subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil];
PHAssetCollection *assetCollection = result.firstObject;
PHFetchResult *assetsFetchResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:nil];
for (int i = index; i<[assets count]; i++) {
PHAsset *asset = assets[i];
NSArray *resources = [PHAssetResource assetResourcesForAsset:asset];
NSString *name = (resources.count > 0) ? [(PHAssetResource*)resources.firstObject originalFilename] : nil;
NSLog(@"%i: %@", i, name);
}
When using undocumented methods to get the file name, such as [asset valueForKey@"filenamme"]
or the PHImageFileURLKey
key of the info
dictionary returned by the PHImageManager
, everything works well (although the name is different than with the originalFilename
and well, it's not reliable since it's not documented).
How come the official method is that unreliable? Is there something I do wrong?
来源:https://stackoverflow.com/questions/41760406/phasset-assetresourcesforasset-fails-when-called-too-often