Filter assets stored on the local device

Problem:

I am trying to fetch only the PHAssets that are stored locally and not in iCloud


Issue:

I am looking to filter them out either in the following api calls:

  1. [PHAssetCollection fetchAssetCollectionsWithType:type subtype:subtype options:fetchOption]
  2. [PHAsset fetchAssetsInAssetCollection:group options:options];


What I tried and didn’t work:

I tried setting PHAssetSourceTypeUserLibrary in the PHFetchOptions

PHFetchOptions *fetchOption = [PHFetchOptions new];

fetchOption.includeAssetSourceTypes = PHAssetSourceTypeUserLibrary;

PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithType:type subtype:subtype options:fetchOption];


What works but is slow:

I used networkAccessAllowed = NO

in the PHImageRequestOptions when calling
[[PHImageManager defaultManager] requestImageDataForAsset: self options: imageRequestOptions resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info)


then in the resultHandler I check if there is imageData or PHImageResultIsInCloudKey in the info dictionary.

That works but that’s an expensive operation, especially for a high number of assets in the iCloud (e.g. 10k)


Back to my question, is there a way to filter out the assets using either:

  1. [PHAssetCollection fetchAssetCollectionsWithType:type subtype:subtype options:fetchOption]
    OR
  2. [PHAsset fetchAssetsInAssetCollection:group options:options];


Thanks in advance,
Alex

Replies

We have the same issue. We can make the code work the slow way, but we want a quickr way. For more modern iOS (10 or later I think) we can check the “locallyAvaliable” on the PHAssetResource, but there are hoops to jump thru because this key doesn't always exist.


We'd like something quick so we can tell which images and videos are local to the device.

Hi,


there is unfortunately no way to do this using the PhotoKit APIs in a quick an efficent manner. The only way that works on iOS8+ higher is to try to request the data for the asset and see if an iCloud download is triggered. This is however very slow for a large set of assets.


Cheers.


Hendrik