如何使用ImagePicker获取图片名称?我得到图像名称,但它只能在模拟器中使用,不能在实际设备中使用
How to get image name using UIImagePicker? I got image name but its working only in simulators not in actual devices
我需要从照片库中选择图片名称。我正在使用 UIImagePicker 并且我得到了图像名称,但它只在模拟器中工作而不在实际设备中工作。
请试试这个:
在你的 UIImagePickerControllerDelegate 中:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let asset = info[UIImagePickerControllerPHAsset] as? PHAsset {
let assetResources = PHAssetResource.assetResources(for: asset)
print(assetResources.first!.originalFilename)
}
dismiss(animated: true, completion: nil)
}
可能对你有帮助。谢谢。
这是完美的工作代码:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
picker.dismissMe();
if let picImgURL = info[UIImagePickerControllerImageURL] as? URL {
let imageName = picImgURL.lastPathComponent
}
}
我需要从照片库中选择图片名称。我正在使用 UIImagePicker 并且我得到了图像名称,但它只在模拟器中工作而不在实际设备中工作。
请试试这个: 在你的 UIImagePickerControllerDelegate 中:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let asset = info[UIImagePickerControllerPHAsset] as? PHAsset {
let assetResources = PHAssetResource.assetResources(for: asset)
print(assetResources.first!.originalFilename)
}
dismiss(animated: true, completion: nil)
}
可能对你有帮助。谢谢。
这是完美的工作代码:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
picker.dismissMe();
if let picImgURL = info[UIImagePickerControllerImageURL] as? URL {
let imageName = picImgURL.lastPathComponent
}
}