How to Not fetch screenshots from Photos library iOS

Viewed 399

I am trying to fetch Photos from Photo library and I want to show only images from the Photo Library. I do not want to show videos.

I am using PHFetchOptions to fetch PHAssets from Photos library. Also I have provided a predicate to fetch only the photos. Below is my code:

let allPhotosOptions = PHFetchOptions()
allPhotosOptions.predicate = NSPredicate(format: "mediaType == 1")
allPhotosOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
fetchResult = PHAsset.fetchAssets(with: allPhotosOptions)

However I am getting screenshots as well. I do not want to fetch the screenshots. I found that there is a property for that as well. It is called PHAssetMediaSubtype.photoScreenshot

I updated the predicate not to fetch screenshots but it doesn't seem to work. So many of my other images are also being filtered which are not screenshots. below is my updated code for predicate

 allPhotosOptions.predicate = NSPredicate(format: "mediaType == 1 AND mediaSubtypes != %d", PHAssetMediaSubtype.photoScreenshot.rawValue)
2 Answers

Have you tried this? It seems like saying NOT on a NSPredicate is quite untrivial

allPhotosOptions.predicate = NSPredicate(format: "(mediaType == 1) AND ((mediaSubtype & %d) != 0)", PHAssetMediaSubtype.photoScreenshot.rawValue)

Got this snippet from this Apple Developer forums post

Try this, should work for your case:

allPhotosOptions.predicate = NSPredicate(format: "mediaType == \(PHAssetMediaType.image.rawValue) AND !((mediaSubtype & \(PHAssetMediaSubtype.photoScreenshot.rawValue)) == \(PHAssetMediaSubtype.photoScreenshot.rawValue))")
Related