iCloud中照片的Swift UIImagePickerController问题并允许编辑

Posted

技术标签:

【中文标题】iCloud中照片的Swift UIImagePickerController问题并允许编辑【英文标题】:Swift UIImagePickerController problem with photo in iCloud and allowsEditing 【发布时间】:2020-07-14 06:55:49 【问题描述】:

我需要帮助。 我使用 UIImagePickerController 来加载头像。我可以从相机拍照,你可以选择一张照片。 我的照片部分存储在 iCloud 中,仅在设备上预览,部分在本地。 选择本地照片或从相机时,我们进入区域选择屏幕,图像裁剪。 因此,如果照片存储在 iCloud 中,则会发生以下情况:

我选择一张照片,点击完成 - 窗口关闭,但没有照片。

我的代码:

从库中选择照片的代码

func openMediaDialog() 
    
        phphotoLibrary.requestAuthorization(status in
                if status == .authorized
                    DispatchQueue.main.async 
                        self.imagePicker.sourceType = UIImagePickerController.SourceType.photoLibrary
                        //self.imagePicker.navigationItem.setHidesBackButton(false, animated: false)
                        //If you dont want to edit the photo then you can set allowsEditing to false
                        self.imagePicker.allowsEditing = true
                        self.imagePicker.delegate = self
                        self.present(self.imagePicker, animated: true, completion: nil)
                    
                    
                 else if status == .denied 
                
                    self.alertCameraAccessNeeded(title: "Необходим доступ к фото!", message: "Для загрузки аватара необходим доступ к фото. Разрешить?")
                
        )
    
    

代表:

override func imagePickerController(_ picker: UIImagePickerController,
                           didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) 
    /*
     Get the image from the info dictionary.
     If no need to edit the photo, use `UIImagePickerControllerOriginalImage`
     instead of `UIImagePickerControllerEditedImage`
     */
    
    guard let selectedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage else 
        fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
        self.view.makeToast("Проблема с выбором изображения")
        
    
    
    
    
    print(selectedImage)
....

下一步 - 上传图片

我请你告诉我如何确保照片是从 icloud 下载、根据需要裁剪并根据通常情况进一步加载到服务器的。

【问题讨论】:

【参考方案1】:

如果您的照片在 iCloud 上,您将获得 PHAsset.. 使用此功能获取照片

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]) 
        guard let phAsset = info[.phAsset] as? 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: phAsset, targetSize: size, contentMode: .aspectFit, options: options)  [weak self] (image, info) in
            if let s = self, let image = image 
                // use this image
            
        
    

【讨论】:

以上是关于iCloud中照片的Swift UIImagePickerController问题并允许编辑的主要内容,如果未能解决你的问题,请参考以下文章

iCloud中的CoreData和照片问题[重复]

iphone怎么查询照片是不是存在ic上

如何在使用照片框架从照片中选择图像时在选择器中隐藏 iCloud 图像

如何读取 iCloud/iTunes 同步的照片

苹果13icloud里的视频怎么取下来

苹果5s 怎么在手机上查看icloud里的照片和视频?