Hi all,
It would seem that this is a clear bug in the Photos framework: using PHFetchOptions with a predicate works great for *selecting* particular mediaSubtypes, but as soon as one tries to *exclude* a subtype, nothing is returned. Better explained with code:
// control: entire library
let library = PHAsset.fetchAssetsWithMediaType(.Image, options: PHFetchOptions())
print("Entire library: \(library.count)")
// expected result: PHAssetMediaSubtype.PhotoScreenshot *only*
// actual: works fine
let options1 = PHFetchOptions()
options1.predicate = NSPredicate(format: "(mediaSubtype & %d) != 0", PHAssetMediaSubtype.PhotoScreenshot.rawValue)
let result1 = PHAsset.fetchAssetsWithMediaType(.Image, options: options1)
print("Screenshots: \(result1.count)")
// expected result: all types *except for* PHAssetMediaSubtype.PhotoScreenshot
// actual: returns nothing
let options2 = PHFetchOptions()
options2.predicate = NSPredicate(format: "(mediaSubtype & %d) == 0", PHAssetMediaSubtype.PhotoScreenshot.rawValue)
let result2 = PHAsset.fetchAssetsWithMediaType(.Image, options: options2)
print("Not screenshots: \(result2.count)")
// expected result1 + result2 = library
assert(result1.count + result2.count == library.count)
Output on a fresh simulator with one imported screenshot:
Entire library: 12
Screenshots: 1
Not screenshots: 0
assertion failed: : file /Users/jc/Desktop/LibraryPredicate/LibraryPredicate/ViewController.swift, line 36
Assertion fails on device as well.
This would appear to be a common issue:
https://forums.developer.apple.com/message/110146#110146
http://stackoverflow.com/questions/35229279/phfetchoptions-mediasubtype-predicate
I'd love to report this as a bug, but the bug reporter is not responding right now. *sigh*