从iOS捕获的图像在哪里?

时间:2015-12-27 06:53:06

标签: swift camera photo

在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会给我一个字符串,这就是为什么我认为这可能是图像,但似乎它不会起作用。

1 个答案:

答案 0 :(得分:1)

您可以这样说:

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是一个可以从类中的任何位置访问的变量。因此,您可以从所有功能和处理程序访问它。 info[UIImagePickerControllerOriginalImage] as? UIImage是来自相机的图像,在代码中,您将变量photo更改为来自相机的图像。现在,来自相机的图像可以在以后的任何地方使用。