从iPhone相机获取图像路径使用UIImagePickerController返回nil

时间:2023-01-22 18:00:44

I am trying to get the image path from camera using UIImagePickerController

我试图使用UIImagePickerController从相机获取图像路径

here is the code :

这是代码:

 @IBAction func takePhoto(sender: UIButton) {
        imagePicker =  UIImagePickerController()
        imagePicker?.allowsEditing = true
        imagePicker?.delegate = self
        imagePicker?.sourceType = .Camera

        presentViewController(imagePicker!, animated: true, completion: nil)
    }

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        let imageURL = info[UIImagePickerControllerReferenceURL] as? NSURL
        print(" Path \(imageURL)")
        imageView?.contentMode = .ScaleAspectFit
        imageView?.image = info[UIImagePickerControllerOriginalImage] as?        UIImage
        imagePicker?.dismissViewControllerAnimated(true, completion: nil)
    }

any help ?

任何帮助?

1 个解决方案

#1


0  

Delegate didFinishPickingMediaWithInfo gets called after a pic is taken using the camera. UIImagePickerControllerReferenceURL object will be nil as the image has not been saved to the camera roll yet.

在使用相机拍摄照片后调用didFinishPickingMediaWithInfo。 UIImagePickerControllerReferenceURL对象将为nil,因为图像尚未保存到相机胶卷。

To get rid of this situation, you need to first save the image and then use the url of the path you saved. You can however use UIImagePickerControllerOriginalImage property on info to show the taken image in your application. Hope this helps!

要摆脱这种情况,您需要先保存图像,然后使用您保存的路径的URL。但是,您可以在info上使用UIImagePickerControllerOriginalImage属性来显示应用程序中的拍摄图像。希望这可以帮助!

#1


0  

Delegate didFinishPickingMediaWithInfo gets called after a pic is taken using the camera. UIImagePickerControllerReferenceURL object will be nil as the image has not been saved to the camera roll yet.

在使用相机拍摄照片后调用didFinishPickingMediaWithInfo。 UIImagePickerControllerReferenceURL对象将为nil,因为图像尚未保存到相机胶卷。

To get rid of this situation, you need to first save the image and then use the url of the path you saved. You can however use UIImagePickerControllerOriginalImage property on info to show the taken image in your application. Hope this helps!

要摆脱这种情况,您需要先保存图像,然后使用您保存的路径的URL。但是,您可以在info上使用UIImagePickerControllerOriginalImage属性来显示应用程序中的拍摄图像。希望这可以帮助!