Swift 4 didFinishPickingMediaWithInfo 保存图像

Swift 4 didFinishPickingMediaWithInfo save image

我正在使用 UIImagePickerController 来使用我的相机:

@objc func toggle() {
        if UIImagePickerController.isSourceTypeAvailable(.camera) {

            //Define UIImagePickerController variable

            let imagePicker = UIImagePickerController()

            //Assign the delegate

            imagePicker.delegate = self

            //Set image picker source type

            imagePicker.sourceType = .camera

            //Allow Photo Editing

            imagePicker.allowsEditing = true

            //Present camera

            UIApplication.topViewController()?.present(imagePicker, animated: true, completion: nil)
        }
    }

现在我正在尝试捕获使用 didFinishPickingMediaWithInfo 方法拍摄的图像,我在网上得到了这个例子:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

        let imageUrl          = info[UIImagePickerControllerOriginalImage] as! NSURL
        let imageName         = imageUrl.lastPathComponent
        let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
        let photoURL          = NSURL(fileURLWithPath: documentDirectory)
        let localPath         = photoURL.appendingPathComponent(imageName!)
        let image             = info[UIImagePickerControllerOriginalImage]as! UIImage
        let data              = UIImagePNGRepresentation(image)

        do
        {
            try data?.write(to: localPath!, options: Data.WritingOptions.atomic)
        }
        catch
        {
            // Catch exception here and act accordingly
        }

        UIApplication.topViewController()?.dismiss(animated: true, completion: nil);
    }

但我将 UIImagePickerControllerReferenceURL 更改为 UIImagePickerControllerOriginalImage,因为 UIImagePickerControllerReferenceURL 为零。但在我改变之后,我得到了这个致命错误:

Could not cast value of type 'UIImage' (0x1b6b02b58) to 'NSURL'

如何保存从相机拍摄的图像?我做错了什么?

按照下面的方式编写代码,这将为您提供图像。

let image = info[UIImagePickerControllerOriginalImage] as! UIImage

UIImagePickerControllerOriginalImage return 图片不是 NSURL

在iOS11中写下获取图片url的代码。从iOS11开始有UIImagePickerControllerImageURL可用,前面有UIImagePickerControllerMediaURL键获取图片url.

if #available(iOS 11.0, *) {
    if let imageURL = info[UIImagePickerControllerImageURL] as? URL {
        print(imageURL)
    }
} else {
    if let imageUrl = info[UIImagePickerControllerMediaURL] as? URL {
        print(imageUrl)
    }
}

希望对您有所帮助。

正在为 Swift 4.2+

寻找完整实现方法的人
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

    if let pickedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage{
        imageView.image = pickedImage
    }

    imgPicker.dismiss(animated: true, completion: nil)
}

这将 return 你 original image 根据新语法

对于图像URL媒体URL,使用相应的

let imgURL = info[UIImagePickerController.InfoKey.imageURL]
let mediaURL = info[UIImagePickerController.InfoKey.mediaURL]