如何使用资产库在uiimagepickercontroller中获取文件名?

Tec*_*ain 8 uiimagepickercontroller ios swift

我想从中获取文件名UIImagePickerController.我不想使用ALAssetLibrary,因为它在iOS 9中被取消了.请告诉我如何使用它.我使用了下面的代码,但它会像UIImagePickerController往常一样为每个文件返回imagename .

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)
}
Run Code Online (Sandbox Code Playgroud)

Raj*_*jat 18

我建议你使用PhotosFramework来获取图像的名称,下面是获取所选图像名称的代码

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)
}
Run Code Online (Sandbox Code Playgroud)

  • *在iOS 11中不推荐使用PHAsset.fetchAssets(withALAssetURLs:[imageURL],options:nil)*.您有替代品吗? (6认同)
  • 此代码不适用于ios 11.0.3.任何解决方案 (5认同)

Gul*_*han 17

这是我如何在 swift 4 中做到的

if let url = info[UIImagePickerController.InfoKey.imageURL] as? URL {
        fileName = url.lastPathComponent
        fileType = url.pathExtension
    }
Run Code Online (Sandbox Code Playgroud)


小智 9

在iOS 11之前

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let imageURL = info[UIImagePickerControllerReferenceURL] as? URL {
        let result = PHAsset.fetchAssets(withALAssetURLs: [imageURL], options: nil)
        let assetResources = PHAssetResource.assetResources(for: result.firstObject!)

        print(assetResources.first!.originalFilename)
    }

    dismiss(animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)

在iOS 11之后

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)
}
Run Code Online (Sandbox Code Playgroud)

  • 当用户在我的应用程序中打开相机、录制视频然后尝试使用该视频时,无法取回 URL。有任何想法吗? (2认同)

Yai*_*iba 6

2018年5月6日更新

强制解包的选项有时会返回nil,我认为这是因为用户使用相机来捕获新图像而不是从照片库中选择一个.当我看到nil时,我更新了代码以返回生成的名称.

原始答案

这对我filename有用,而不必依赖键(请原谅强行解包选项:))

extension MyViewController : UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        guard let image = info[UIImagePickerControllerEditedImage] as? UIImage else {
            DDLogDebug("No image chosen")
            return
        }

        if let url = info[UIImagePickerControllerReferenceURL] as? URL {
            let assets = PHAsset.fetchAssets(withALAssetURLs: [url], options: nil)
            if let firstAsset = assets.firstObject,
            let firstResource = PHAssetResource.assetResources(for: firstAsset).first {
                fileName = firstResource.originalFilename
            } else {
                fileName = generateNameForImage()
            }
        } else {
            fileName = generateNameForImage()
        }

        DDLogDebug("File name = \(fileName)")

        dismiss(animated: true)
    }

    func generateNameForImage() -> String {
        return "IMG_random_string"
    }

}
Run Code Online (Sandbox Code Playgroud)


Fun*_*Kat 5

斯威夫特 5、iOS 11+

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    let photo = info[.phAsset] as? PHAsset
    let filename = photo?.value(forKey: "filename") as! String
}
Run Code Online (Sandbox Code Playgroud)