这是我正在尝试使用的代码。我得到的只是一个错误:
类型“UIImagePickerController.InfoKey”(又名“NSString”)没有成员“editedImage”。
我的进口:
import UIKit
import Photos
import FirebaseAuth
import JGProgressHUD
使用:
// 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)
}
}
将 '.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)
}
}