I am trying to retrieve images from photo library using swift. This is the below code that is working for iOS:
func fetchPHAssets() -> [PHAsset] {
var assets = [PHAsset]()
let options = PHFetchOptions.init()
options.predicate = NSPredicate.init(format: "(mediaSubtype & %d) != 0", PHAssetMediaSubtype.photoScreenshot.rawValue)
let fetchAssets: PHFetchResult = PHAsset.fetchAssets(with: .image, options: options)
fetchAssets.enumerateObjects({ (asset, _, _) in
assets.append(asset)
})
return assets
}
Photos framework for Mac app doesn't have PHAsset.fetchAssets method that takes subtypes. So I tried below code to get the results:
func fetchPHAssets() -> [PHAsset] {
var assets = [PHAsset]()
let options = PHFetchOptions.init()
//retrieve only screen shots
options.predicate = NSPredicate.init(format: "(mediaSubtype & %d) != 0", PHAssetMediaSubtype.photoScreenshot.rawValue)
let fetchAssets: PHFetchResult = PHAsset.fetchAssets(in: PHAssetCollection.init(), options: nil)
fetchAssets.enumerateObjects({ (asset, _, _) in
assets.append(asset)
})
return assets
}
but the result is 0.
I tried looking into documentation but it is very poorly written with no explanation. Explored online was able to find examples only for iOS app. Any kind of help is appreciated. Thanks.