When loading images from the photo library via PhotoKit, it can happen that the image is not downloaded from iCloud. In this case, a low-resolution "placeholder" version of that image is available locally if you browse it in the photos App:
However, when I try to get the same photo to display in an app, the photo that I get from PHAssetManager
will be at maximum 120
units wide (or high). This looks very blurry and is completely unusable except for a thumbnail.
I have already tried
PHImageManagerMaximumSize
as the target sizeresizeMode = .none
and.exact
- all the
deliveryMode
's requestImageDataAndOrientation
- to read the image data in a different way, e.g. by
privateFileURL
property on the asset
I wanted to ask if anyone has come across this issue and maybe worked around it.
The behavior can be easily reproduced with some PhotoKit example app, for example from these course materials. This is the code I used to produce the below image:
extension UIImageView {
func fetchImageAsset(_ asset: PHAsset?, targetSize size: CGSize, contentMode: PHImageContentMode = .aspectFill, completionHandler: ((Bool) -> Void)?) {
let options = PHImageRequestOptions()
options.deliveryMode = .opportunistic
options.resizeMode = .none
options.isNetworkAccessAllowed = false
// 1
guard let asset = asset else {
completionHandler?(false)
return
}
// 2
let resultHandler: (UIImage?, [AnyHashable: Any]?) -> Void = { image, info in
if let image = image {
self.image = image
}
completionHandler?(true)
}
// 3
PHImageManager.default().requestImage(
for: asset,
targetSize: size,
contentMode: contentMode,
options: options,
resultHandler: resultHandler)
}
}