iosswiftphassetphotosframeworkphfetchoptions

NSPredicate to not include Loop and Bounce Live Photos


In my app I only show assets the user can edit, so I only show photos - no videos. New in iOS 11 Live Photos have two effects that effectively turn the photo into a video - Loop and Bounce. These Live Photos cannot be edited in the Photos app - the plugins button is disabled. I need to filter those out in my PHFetchResult. But mediaType of image still includes these 'live videos'. How can I exclude those from the fetch? Maybe something to do with the playbackStyle of PHAsset?

let photoLibraryFetchResult = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .smartAlbumUserLibrary, options: nil)  
let assetCollection = photoLibraryFetchResult.firstObject!  

let imagesOnlyFetchOptions = PHFetchOptions()  
imagesOnlyFetchOptions.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.image.rawValue)  

let assetsFetchResults = PHAsset.fetchAssets(in: assetCollection, options: imagesOnlyFetchOptions)

Solution

  • Try looking at the playbackStyle value. (Read more here)

    The enum has the following options:

    case image
    case imageAnimated
    case livePhoto
    case unsupported
    case video
    case videoLooping