[英]Error on image capture with camera mode
I'm working on image picker with camera
and gallery
and using following code to get image captured from camera but imageurl and image name
is nil.I'm testing on mobile with os version of 11.2.2. 我正在使用带有camera
和gallery
图像选择器,并使用以下代码从相机获取图像,但imageurl and image name
为nil。我正在使用11.2.2操作系统版本在移动设备上进行测试。
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
let imageUrl = info[UIImagePickerControllerReferenceURL] as? NSURL
let imageName = imageUrl?.lastPathComponent
let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
let photoURL = NSURL(fileURLWithPath: documentDirectory)
let localPath = photoURL.appendingPathComponent(imageName!)
if !FileManager.default.fileExists(atPath: localPath!.path) {
do {
try UIImageJPEGRepresentation(image, 1.0)?.write(to: localPath!)
print("file saved")
}catch {
print("error saving file")
}
}
else {
print("file already exists")
}
}
像这样使用UIImagePickerControllerImageURL
代替UIImagePickerControllerReferenceURL
:
let imageUrl = info[UIImagePickerControllerImageURL] as? NSURL
You will not get any imageUrl
or imageName
because you are using the camera mode and that image is still not saved in your gallery and hence no image path is created for that camera clicked pic. 您将不会获得任何imageUrl
或imageName
因为您正在使用相机模式,并且该图像仍未保存在图库中,因此不会为该相机单击的图片创建任何图像路径。 So they will be nil. 因此他们将为零。 You can only get the UIImage
object info[UIImagePickerControllerOriginalImage]
in this case. 在这种情况下,您只能获取UIImage
对象info[UIImagePickerControllerOriginalImage]
。
If you pick the image from photo album , then you will get everyhting. 如果您从相册中挑选图像,那么您将一事无成。 You can get the url by info[UIImagePickerControllerImageURL]
and you can grab the name using the last path component. 您可以通过info[UIImagePickerControllerImageURL]
获取url,并且可以使用最后一个路径组件来获取名称。
Change your imagePickerController source to photo imagePickerController.sourceType = .photoLibrary
and then you will get the urls. 将您的imagePickerController源更改为photo imagePickerController.sourceType = .photoLibrary
,然后您将获得这些URL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.