没有获取图像路径
Not getting image path
我正在使用 UIImagePickerController
来选择图像我想要所选图像的路径,但我无法获取它。
我在代码中尝试的内容:
func handleImg() {
let picker = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = true
present(picker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
var selectedImageFromPicer : UIImage?
if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage {
selectedImageFromPicer = editedImage
} else if let originalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage {
selectedImageFromPicer = originalImage
}
if let selectedImage = selectedImageFromPicer {
img.image = selectedImage
}
if let imageURL = info[UIImagePickerControllerReferenceURL] as? URL {
let result = PHAsset.fetchAssets(withALAssetURLs: [imageURL], options: nil)
let asset = result.firstObject
var imgStr = asset?.value(forKey: "filename")
print(imgStr!)
}
dismiss(animated: true, completion: nil)
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
print("canceled picker")
dismiss(animated: true, completion: nil)
}
}
通过上面的代码我只能得到图像的名称。我要图片路径。
如果您打印 info
对象,您将看到所有可用于访问该字典值的键。图片路径key是UIImagePickerControllerImageURL
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
guard let imageUrl = info["UIImagePickerControllerImageURL"] as? URL else {
return
}
print(imageUrl.path) // prints the path
picker.dismiss(animated: true, completion: nil)
}
以下代码可以帮助您获取图片路径。
显示 UIImagePickerController 的代码。
@IBAction func showPicker(_ sender: Any) {
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = UIImagePickerControllerSourceType.savedPhotosAlbum
picker.allowsEditing = false
present(picker, animated: true, completion: nil)
}
委托方法 didFinishPickingMediaWithInfo.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
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!)
//Check these values
self.dismiss(animated: true, completion: nil);
}
我正在使用 UIImagePickerController
来选择图像我想要所选图像的路径,但我无法获取它。
我在代码中尝试的内容:
func handleImg() {
let picker = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = true
present(picker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
var selectedImageFromPicer : UIImage?
if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage {
selectedImageFromPicer = editedImage
} else if let originalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage {
selectedImageFromPicer = originalImage
}
if let selectedImage = selectedImageFromPicer {
img.image = selectedImage
}
if let imageURL = info[UIImagePickerControllerReferenceURL] as? URL {
let result = PHAsset.fetchAssets(withALAssetURLs: [imageURL], options: nil)
let asset = result.firstObject
var imgStr = asset?.value(forKey: "filename")
print(imgStr!)
}
dismiss(animated: true, completion: nil)
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
print("canceled picker")
dismiss(animated: true, completion: nil)
}
}
通过上面的代码我只能得到图像的名称。我要图片路径。
如果您打印 info
对象,您将看到所有可用于访问该字典值的键。图片路径key是UIImagePickerControllerImageURL
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
guard let imageUrl = info["UIImagePickerControllerImageURL"] as? URL else {
return
}
print(imageUrl.path) // prints the path
picker.dismiss(animated: true, completion: nil)
}
以下代码可以帮助您获取图片路径。
显示 UIImagePickerController 的代码。
@IBAction func showPicker(_ sender: Any) {
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = UIImagePickerControllerSourceType.savedPhotosAlbum
picker.allowsEditing = false
present(picker, animated: true, completion: nil)
}
委托方法 didFinishPickingMediaWithInfo.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
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!)
//Check these values
self.dismiss(animated: true, completion: nil);
}