Check given PHAsset is iCloud asset?

后端 未结 6 1169
再見小時候
再見小時候 2021-02-02 14:16

I\'m trying to get PhAsset object. I want to segregate iCloud assets. Here is my code,

PHFetchResult *cloudAlbums = [PHAssetCollection fetchAssetCollectionsWithT         


        
6条回答
  •  -上瘾入骨i
    2021-02-02 15:11

    Following is a method you can implement to acquire all videos in the Videos folder of the Photos app, which uses a predicate with the PHFetchRequest to filter only videos stored on the iPhone itself, and not in iCloud:

    // Collect all videos in the Videos folder of the Photos app
    - (PHFetchResult *)assetsFetchResults {
        __block PHFetchResult *i = self->_assetsFetchResults;
        if (!i) {
            static dispatch_once_t onceToken;
            dispatch_once(&onceToken, ^{
                PHFetchOptions *fetchOptions = [PHFetchOptions new];
                fetchOptions.predicate = [NSPredicate predicateWithFormat:@"(sourceType & %d) != 0", PHAssetSourceTypeUserLibrary];
                PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumVideos options:fetchOptions];
                PHAssetCollection *collection = smartAlbums.firstObject;
                if (![collection isKindOfClass:[PHAssetCollection class]]) collection = nil;
                PHFetchOptions *allPhotosOptions = [[PHFetchOptions alloc] init];
                allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
                i = [PHAsset fetchAssetsInAssetCollection:collection options:allPhotosOptions];
                self->_assetsFetchResults = i;
            });
        }
    
        return i;
    }
    

    Apple's documentation on PHFetchResult states that only a subset of attributes can be used with a predicate; so, if the above code does not work for you, remove the PHFetchOptions predicate, and replace the corresponding reference in the PHFetchRequest to nil:

    // Collect all videos in the Videos folder of the Photos app
    - (PHFetchResult *)assetsFetchResults {
        __block PHFetchResult *i = self->_assetsFetchResults;
        if (!i) {
            static dispatch_once_t onceToken;
            dispatch_once(&onceToken, ^{
                PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumVideos options:nil];
                PHAssetCollection *collection = smartAlbums.firstObject;
                if (![collection isKindOfClass:[PHAssetCollection class]]) collection = nil;
                PHFetchOptions *allPhotosOptions = [[PHFetchOptions alloc] init];
                allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
                i = [PHAsset fetchAssetsInAssetCollection:collection options:allPhotosOptions];
                self->_assetsFetchResults = i;
            });
        }
    
        return i;
    }
    

    Then, add this line:

    // Filter videos that are stored in iCloud
    - (NSArray *)phAssets {
        NSMutableArray *assets = [NSMutableArray arrayWithCapacity:self.assetsFetchResults.count];
        [[self assetsFetchResults] enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) {
            if (asset.sourceType == PHAssetSourceTypeUserLibrary)
                     [assets addObject:asset];
         }];
    
        return [NSArray arrayWithArray:(NSArray *)assets];
    }
    

提交回复
热议问题