ios - 使用未解析的标识符 'image'

标签 ios swift camera

我有一个小问题。我正在尝试在 Swift Xcode 中制作相机,但是,我遇到了一个问题,那就是“ImagePicked.image = image”一直显示错误。我不知道为什么会这样。 Photo of the interface of the app

import UIKit

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    @IBOutlet weak var ImagePicked: UIImageView!

    @IBAction func openCamera(_ sender: Any) {
        if UIImagePickerController.isSourceTypeAvailable(.camera) { //Is the camera an available source type?
            let imagePicker = UIImagePickerController() //Declare variable imagePicker
            imagePicker.delegate = self
            imagePicker.sourceType = .camera;
            imagePicker.allowsEditing = false //Tell Image Pickr not to edit camptured photo
            self.present(imagePicker, animated: true, completion: nil) //Show the photo to the user
        }
        func openLibrary(_ sender: Any) {
            if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) { //Check if device has access to photo library
                let imagePicker = UIImagePickerController() //Set up variable imagePicker
                imagePicker.delegate = self
                imagePicker.sourceType = .photoLibrary; //Set the source type to library
                imagePicker.allowsEditing = true //Allow editing, so the user can move and crop their photo
                self.present(imagePicker, animated: true, completion: nil) //Show UIImagePickerController to the user
            }
        }
        func saveImage(_ sender: Any) {
            let ImageData = ImagePicked.image!.jpegData(compressionQuality: 0.6)
            let compressedJPGImage = UIImage(data: ImageData!)
            UIImageWriteToSavedPhotosAlbum(compressedJPGImage!, nil, nil, nil)
            let alertController = UIAlertController(title: "Complete", message: "Your image has been saved.", preferredStyle: .alert)
            let okAction = UIAlertAction(title: "OK", style: .default); alertController.addAction(okAction); self.present(alertController, animated: true, completion: nil)
        }
        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
            guard (info[.originalImage] as? UIImage) != nil else {
                fatalError ("Expected a dictionary containtaining an image, but was provided with the following: \(info)")
                ImagePicked.image = image //PROBLEM
                dismiss(animated: true, completion: nil)
            }
        }

        func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
        }
    }
}

最佳答案

更新 didFinishPickingMediaWithInfo 如下,

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
      guard let image = info[.originalImage] as? UIImage else { return }
      ImagePicked.image = image
      picker.dismiss(animated: true, completion: nil)
}

正如@rmaddy 评论的那样,您应该重新安排如下方法,

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    @IBOutlet weak var ImagePicked: UIImageView!

    @IBAction func openCamera(_ sender: Any) {
        if UIImagePickerController.isSourceTypeAvailable(.camera) { //Is the camera an available source type?
            let imagePicker = UIImagePickerController() //Declare variable imagePicker
            imagePicker.delegate = self
            imagePicker.sourceType = .camera;
            imagePicker.allowsEditing = false //Tell Image Pickr not to edit camptured photo
            self.present(imagePicker, animated: true, completion: nil) //Show the photo to the user
        }
    }
   func openLibrary(_ sender: Any) {
            if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) { //Check if device has access to photo library
                let imagePicker = UIImagePickerController() //Set up variable imagePicker
                imagePicker.delegate = self
                imagePicker.sourceType = .photoLibrary; //Set the source type to library
                imagePicker.allowsEditing = true //Allow editing, so the user can move and crop their photo
                self.present(imagePicker, animated: true, completion: nil) //Show UIImagePickerController to the user
            }
        }
   func saveImage(_ sender: Any) {
            let ImageData = ImagePicked.image!.jpegData(compressionQuality: 0.6)
            let compressedJPGImage = UIImage(data: ImageData!)
            UIImageWriteToSavedPhotosAlbum(compressedJPGImage!, nil, nil, nil)
            let alertController = UIAlertController(title: "Complete", message: "Your image has been saved.", preferredStyle: .alert)
            let okAction = UIAlertAction(title: "OK", style: .default); alertController.addAction(okAction); self.present(alertController, animated: true, completion: nil)
        }

  func viewDidLoad() {
       super.viewDidLoad()
       // Do any additional setup after loading the view, typically from a nib.
   }
}

关于ios - 使用未解析的标识符 'image',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58004627/

相关文章:

ios - 如何快速计算多行文本的最佳标签宽度

ios - 无法使用 PFFacebookUtilsV4 获取 session token

swift - 为什么videoview宽度大小是一半?

ios - iOS 中的摄像头运动感应

ios - 类不符合 Swift 中键的键值编码

iphone - 如何构建类似 iPad App 的终端

ios - 从按钮删除 TableView 中的一行

ios - 如何在收到时更改 iOS 通知消息?

ios - 1 :1 width:height constraint to UIButton programmatically

iphone - 如何在 iOS 中拍照并将其作为电子邮件附件发送