Roi Mulia
Roi Mulia

Reputation: 5896

Fetch only Live Photos using Photos Framework dosen't work

I'm trying to fetch only Live Photos out of the device albums. I found this Objective C code, but for some reason when converting it to Swift to doesn't compile.

PHFetchOptions *options = [[PHFetchOptions alloc] init];
options.predicate = [NSPredicate predicateWithFormat: @"(mediaSubtype == %ld)", PHAssetMediaSubtypePhotoLive];
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey: @"creationDate" ascending: NO]];
PHFetchResult<PHAsset *> *assetsFetchResults = [PHAsset fetchAssetsWithMediaType: PHAssetMediaTypeImage options: options];

Swift:

 fetchOptions.predicate = NSPredicate(format: "mediaSubtype == %ld", PHAssetMediaSubtype.PhotoLive)

throws compile error :

 Argument type 'PHAssetMediaSubtype' does not conform to expected type 'CVarArgType'

Any suggestions?

Upvotes: 1

Views: 1291

Answers (1)

Paulw11
Paulw11

Reputation: 114992

You can't supply a Swift enumeration value to that method - it needs a long (as is implied by the %ld format string), so use

fetchOptions.predicate = NSPredicate(format: "mediaSubtype == %ld", PHAssetMediaSubtype.PhotoLive.rawValue)

to access the underlying integer value of the enumeration value

Upvotes: 6

Related Questions