如何从UIImage提取图像文件名

时间:2018-08-16 09:09:54

标签: swift uiimage uiimagepickercontroller

我有<UIImage: 0x6040000b9800> , {1668, 2500}的照片来自画廊。但是我需要在String中使用imagename。有人帮我迅速解决4吗?我在下面给出了代码:

extension PhotoViewController: ImagePickerDelegate {
func wrapperDidPress(_ imagePicker: ImagePickerController, images: [UIImage]) {

}

func doneButtonDidPress(_ imagePicker: ImagePickerController, images: [UIImage]) {
    self.image = images[0]



    let navigati:PhotoDescriptorController = storyboard!.instantiateViewController(withIdentifier: StoryBoard.PhotoDescriptorController) as! PhotoDescriptorController

    navigati.imageDest = self.image
    self.navigationController?.pushViewController(navigati, animated: true)

    imagePicker.dismiss(animated: true, completion: nil)
}

func cancelButtonDidPress(_ imagePicker: ImagePickerController) {
    imagePicker.dismiss(animated: true, completion: nil)
}

2 个答案:

答案 0 :(得分:0)

您是否尝试过使用Photo框架来获取imageName?

查看此示例。

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

答案 1 :(得分:0)

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let picURL = info[UIImagePickerControllerReferenceURL] as? URL {
        let imgData = PHAsset.fetchAssets(withALAssetURLs: [picURL], options: nil)
        let asset = imgData.firstObject
        print(asset?.value(forKey: "filename"))

    }

    dismiss(animated: true, completion: nil)
}