我收到错误:“UIImagePickerController.InfoKey”(又名“NSString”)没有成员“editedImage”

问题描述 投票:0回答:1

这是我正在尝试使用的代码。我得到的只是一个错误:

类型“UIImagePickerController.InfoKey”(又名“NSString”)没有成员“editedImage”。

我的进口:

import UIKit
import Photos
import FirebaseAuth
import JGProgressHUD

使用:

  • XCode 版本 16.0 (16A242d)
  • swift-驱动程序版本:1.115 Apple Swift 版本 6.0 (swiftlang-6.0.0.9.10 clang-1600.0.26.2)
// Delegate method to handle the selection
extension RegisterViewController {
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let editedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
            print("Edited Image: \(editedImage)")
        } else if let originalImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
            print("Original Image: \(originalImage)")
        }
        picker.dismiss(animated: true, completion: nil)
    }

    // Handle cancellation
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true, completion: nil)
    }
}
swift uikit uiimagepickercontroller
1个回答
0
投票

将 '.editedImage' 替换为 .editedImage,因为 UIImagePickerController.InfoKey 使用 Swift 枚举,而不是字符串。

import UIKit

class YourViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    func presentImagePicker() {
        let imagePickerController = UIImagePickerController()
        imagePickerController.delegate = self
        imagePickerController.sourceType = .photoLibrary
        imagePickerController.allowsEditing = true // Enable editing
        present(imagePickerController, animated: true, completion: nil)
    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        // Retrieve the edited image
        if let editedImage = info[.editedImage] as? UIImage {
            print("Edited image selected")
            // Do something with the edited image
        } else if let originalImage = info[.originalImage] as? UIImage {
            print("Original image selected")
            // Do something with the original image
        }

        // Dismiss the picker
        picker.dismiss(animated: true, completion: nil)
    }

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        // Handle cancel action
        picker.dismiss(animated: true, completion: nil)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.