UIImagePickerController和iCloud照片 [英] UIImagePickerController and iCloud photos

查看:287
本文介绍了UIImagePickerController和iCloud照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

切换到iCloud Photo之后,似乎UIImagePickerController返回的某些图像非常模糊.图像似乎是从iCloud Photo拍摄的.

After switching to iCloud Photo, it seems that some of the images returned by UIImagePickerController are very blur. It looks like the image are taken from iCloud Photo.

我是否能够检索原始图像,或滤除iCloud Photo图像,还是必须切换到其他框架来执行UIImagePickerController的工作?

Am I able to retrieve the original image, or filter off iCloud Photo images, or do I have to switch to other frameworks to do what UIImagePickerController do?

推荐答案

从症状来看,我假设您正在使用类似的方法来加载图像:

From the symptoms, I'm going to assume you are using something like this to load your images:

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
            loadImageInMemory(image)
        }
        picker.dismiss(animated: true, completion: nil)
        self.presentingViewController?.dismiss(animated: true, completion: nil)
    }

其中loadImageInMemory是处理图像的功能.

where loadImageInMemory is a function where you process the image.

事实证明,如果使用该方法,则在iCloud上存储的图像检索质量可能会低于原始图像.验证情况的一种方法是更改​​照片"设置.在设置"应用中:

It turns out that if you use that method, images stored over iCloud may be retrieved in lower quality than the original. A way to verify that this is the case is to change your Photos settings. From the Settings app:

Settings -> Photos -> Download and Keep Originals

这可以解决问题,但是当然不是可取的.如果要继续使用照片,而不是实施自己的iCloud解决方案,而保留Optimize iPhone Storage设置,则可以使用

This would fix the issue, but of course it's not desirable. If you want to keep using Photos, instead of implementing your own iCloud solution, while keeping the Optimize iPhone Storage setting, you can use PhotoKit to retrieve the original image.

改为使用此代码:

import Photos

// ...

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        // it will be loaded asynchronously
        loadImageFromPicker(info: info)
        picker.dismiss(animated: true, completion: nil)
        self.presentingViewController?.dismiss(animated: true, completion: nil)
    }

    private func loadImageFromPicker(info: [UIImagePickerController.InfoKey : Any]) {
        var phAsset: PHAsset?
        if #available(iOS 11.0, *) {
            phAsset = info[UIImagePickerController.InfoKey.phAsset] as? PHAsset
        } else {
            // Fallback on earlier versions
            if let referenceURL = info[UIImagePickerController.InfoKey.referenceURL] as? URL {
                let fetchResult = PHAsset.fetchAssets(withALAssetURLs: [referenceURL], options: nil)
                phAsset = fetchResult.firstObject
            }
        }
        guard let asset = phAsset else {
            return
        }
        // size doesn't matter, because resizeMode = .none
        let size = CGSize(width: 32, height: 32)
        let options = PHImageRequestOptions()
        options.version = .original
        options.deliveryMode = .highQualityFormat
        options.resizeMode = .none
        options.isNetworkAccessAllowed = true
        PHImageManager.default().requestImage(for: asset, targetSize: size, contentMode: .aspectFit, options: options) { [weak self] (image, info) in
            if let s = self, let image = image {
                s.loadImageInMemory(image)
            }
        }
    }

此代码将与本地图像和iCloud图像一起使用.

This code will work with both local images and iCloud images.

这解决了我在使用带有alpha的小型PNG图像时遇到的类似问题.请参阅此其他帖子以供参考.

This has fixed a similar problem I experienced working with small PNG images with alpha. See this other post for reference.

这篇关于UIImagePickerController和iCloud照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆