我想从获取文件名UIImagePickerController。我不想使用ALAssetLibrary,因为它在iOS 9中已弃用。我使用了以下代码,但对于每个文件,它始终将图像名称返回为“ Asset.jpg”。
UIImagePickerController
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { let originalImage = (info[UIImagePickerControllerOriginalImage] as? UIImage)! let url = info[UIImagePickerControllerReferenceURL] as! NSURL imageData = UIImageJPEGRepresentation(originalImage, 100) as NSData? let data = UploadData() data.fileName = url.lastPathComponent picker.dismiss(animated: true, completion: nil) }
我建议您使用PhotosFramework来获取图像的名称,下面是获取所选图像名称的代码
Photos
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { if let imageURL = info[UIImagePickerControllerReferenceURL] as? URL { let result = PHAsset.fetchAssets(withALAssetURLs: [imageURL], options: nil) let asset = result.firstObject print(asset?.value(forKey: "filename")) } dismiss(animated: true, completion: nil) }