从 iOS 捕获的图像在哪里?

Where is the image captured from iOS?

在swift中使用默认相机时,我用它来拍照,然后在 imageView 中显示它:

@IBAction func takePhoto() {
    let picker = UIImagePickerController()

    picker.delegate = self
    picker.sourceType = .Camera

    self.presentViewController(picker, animated: true, completion: nil)
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
    dismissViewControllerAnimated(true, completion: nil)
}

但是我想把我刚拍的图像拍下来并存储在一个常量中,但我不知道我拍的图像是什么。我试过用这个作为图片

UIImage(named: UIImagePickerControllerOriginalImage)

UIImage(named: "spect a string here")UIImagePickerControllerOriginalImage returns 给我一个字符串所以这就是为什么我认为这可能是图像,但它似乎不起作用。

你可以这样说:

var photo = UIImage() // Accessible from anywhere in the class
@IBAction func takePhoto() {
    let picker = UIImagePickerController()
    picker.delegate = self
    picker.sourceType = .Camera
    self.presentViewController(picker, animated: true, completion: nil)
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
    photo = info[UIImagePickerControllerOriginalImage] as? UIImage // Assign `photo` to the image
}

在上面的代码中,photo 是一个可以从 class 中的任何地方访问的变量。因此,您可以从所有函数和处理程序访问它。 info[UIImagePickerControllerOriginalImage] as? UIImage 是来自相机的图像,在代码中,您将变量 photo 更改为来自相机的图像。现在,稍后可以从 class 中的任何地方获取来自相机的图像。