如何获取相机iOS Swift拍摄的图片的路径 [英] how to get path of picture taken by camera ios swift

查看:110
本文介绍了如何获取相机iOS Swift拍摄的图片的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用imagePickerController(_ picker:UIImagePickerController,didFinishPickingMediaWithInfo委托方法来获取用户在图库中选择的图像的URL.但是,当我尝试获取相机拍摄的图像的URL时,返回零.这是我的代码.

I was using the imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo delegate method for getting the url of the image which user chose on photo gallery. But when I try to get URL for image taken by camera its return nill. Here is my code.

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        if (imagePicker.sourceType == UIImagePickerControllerSourceType.camera) {
            let data = UIImagePNGRepresentation(pickedImage)
            UIImageWriteToSavedPhotosAlbum(pickedImage, nil, nil, nil)

            if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
                let imageURL =  info[UIImagePickerControllerReferenceURL] as? NSURL
                print(imageURL)
            }
        }

推荐答案

您所拍摄的图像尚未保存,并且没有任何名称.您需要先保存图像,然后才能获取图像的路径.这就是为什么它返回 nil .

The image you have taken is not saved and has not any name yet. You need to save the image before you can get the path for the image. That´s why it returns nil.

如果您从图片库中选择一张图片而不是用相机拍摄一张图片,则会得到一条路径.

If you select an image from the image library instead of taking one with the camera you´ll get a path.

这篇关于如何获取相机iOS Swift拍摄的图片的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆