PHAsset assetResourcesForAsset fails when called too often

白昼怎懂夜的黑 提交于 2019-12-13 06:10:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!