如何使用资产库在uiimagepickercontroller中获取文件名? [英] How to get file name in uiimagepickercontroller with Asset library?

查看:463
本文介绍了如何使用资产库在uiimagepickercontroller中获取文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 UIImagepickercontroller 获取文件名。我不想使用ALAssetLibrary,因为它在iOS 9中被取消了。请告诉我如何使用它。我有使用下面的代码,但它返回我的imagename为 Asset.jpg 总是为每个文件。

I want to get the file name from UIImagepickercontroller.I do not want to use ALAssetLibrary because it is deperecated in iOS 9.Please tell how to use it.I have used below code but it return me imagename as Asset.jpg always for each file.

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)
  }


推荐答案

我建议你使用照片获取图像名称的框架,下面是获取所选图像名称的代码

I will suggest you to use Photos Framework to get the name of the image, below is the code to get name of selected image

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)
}

这篇关于如何使用资产库在uiimagepickercontroller中获取文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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