使用未解决的标识符“图像”

时间:2019-09-19 06:05:34

标签: 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.
        }
    }
}

1 个答案:

答案 0 :(得分:2)

如下更新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.
   }
}