I want to do some work on a user's photo library. Since the library can be huge, I want to do it in the background. I'm wondering whether it is safe to perform asset fetches (like PHAsset.fetchAssets
) and work on them in the background?
I only need the asset metadata for now.
Would something like this be safe:
class ViewController: UIViewController {
var cachedResult = [Any]()
func doBackgroundCalculationsOnPhotos(completionHandler: ([Any]) -> ()) {
DispatchQueue.global(qos: .userInitiated).async {
let photos = PHAsset.fetchAssets(with: .image, options: nil)
var result = [Any]()
photos.enumerateObjects({ asset, _, _ in
result.append(calculateSomething(asset))
})
DispatchQueue.main.async {
self.cachedResult = result
completionHandler(result)
}
}
}
}
Are there any references to documentation where I could learn about Photos Framework and background access?
Use this method if you only need UIImage from PHAsset . extension PHAsset { func image(targetSize: CGSize, contentMode: PHImageContentMode, options: PHImageRequestOptions?) -> UIImage { var thumbnail = UIImage() let imageManager = PHCachingImageManager() imageManager.
A representation of an image, video, or Live Photo in the Photos library.
Yes, it can take some time to fetch so it might be a good idea to do this in the background as the fetchAssets(with:options:)
method is synchronous.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With