获取从UIImagePickerController中选择的UIImage的URL

moz*_*sky 16 nsurl uiimagepickercontroller ios swift ios8

我正在尝试从照片库中提供用户选择的图像.因此我正在UIImagePickerController选择.但是这里出现了在文件系统中获取其初始URL的问题(我需要这个CKAsset).

我的代码.

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
    let imageURL = info[UIImagePickerControllerReferenceURL] as NSURL
    let path = imageURL.path!
    let imageName = path.lastPathComponent
    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
    let documentDirectory = paths.first as String!
    let localPath = documentDirectory + "/" + imageName

    let imageData = NSData(contentsOfFile: localPath)!
    let image = UIImage(data: imageData)!

    picker.dismissViewControllerAnimated(true, completion: nil)

}
Run Code Online (Sandbox Code Playgroud)

imageURL有点神秘.它描述了资产URL,但不是文件系统中的资产URL.它看起来像:assets-library://asset/asset.JPG?id=B6C0A21C-07C3-493D-8B44-3BA4C9981C25&ext=JPG.

根据该逻辑路径/asset.JPG其中asset.JPG是所述图像的名称.

然后我正在访问我的Documents文件夹并试图找到一个带路径的文件:

/Users/Eugene/Library/Developer/CoreSimulator/Devices/3C5E9A23-8220-4B37-BD14-F1E42EEC2C7C/data/Containers/Data/Application/20EBAAE2-6C6F-4651-A48F-16A222CCB3A2/Documents/asset.JPG

也是隐秘的,但似乎是不存在的图像的真实路径......没有找到具有该路径的图像.这让我感到恶心.

我是否必须从头开始保存图像?或者还有其他方法吗?

我已经使用AssetsLibrary API 查看了几个教程,但我找不到任何有用的方法来解决我的问题.先感谢您!

moz*_*sky 33

好的,我已经解决了这个问题.

您只需抓取图像(info[UIImagePickerControllerOriginalImage] as UIImage)并保存到给定目录即可.如果你只需要保存1张图片就可以了.代码ID如下.

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
    let imageURL = info[UIImagePickerControllerReferenceURL] as NSURL
    let imageName = imageURL.path!.lastPathComponent
    let documentDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as String
    let localPath = documentDirectory.stringByAppendingPathComponent(imageName)

    let image = info[UIImagePickerControllerOriginalImage] as UIImage
    let data = UIImagePNGRepresentation(image)
    data.writeToFile(localPath, atomically: true)

    let imageData = NSData(contentsOfFile: localPath)!
    let photoURL = NSURL(fileURLWithPath: localPath)
    let imageWithData = UIImage(data: imageData)!

    picker.dismissViewControllerAnimated(true, completion: nil)

}
Run Code Online (Sandbox Code Playgroud)

  • 我没有在Swift 3中工作,我转换代码使其编译但返回的路径并未指向所选图像 (5认同)
  • 你能否为Swift 2更新你的答案?谢谢! (3认同)

Jay*_*dip 9

SWIFT 4你可以尝试这个,它正常工作.

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {


    if let imgUrl = info[UIImagePickerControllerImageURL] as? URL{
        let imgName = imgUrl.lastPathComponent
        let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
        let localPath = documentDirectory?.appending(imgName)

        let image = info[UIImagePickerControllerOriginalImage] as! UIImage
        let data = UIImagePNGRepresentation(image)! as NSData
        data.write(toFile: localPath!, atomically: true)
        //let imageData = NSData(contentsOfFile: localPath!)!
        let photoURL = URL.init(fileURLWithPath: localPath!)//NSURL(fileURLWithPath: localPath!)
        print(photoURL)

    }

    APPDEL.window?.rootViewController?.dismiss(animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)


Han*_*zky 5

对于 Swift 5+ ;根据 @Jaydip 回答

if let imgUrl = info[UIImagePickerController.InfoKey.imageURL] as? URL{
        let imgName = imgUrl.lastPathComponent
        let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
        let localPath = documentDirectory?.appending(imgName)

        let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
        let data = image.pngData()! as NSData
        data.write(toFile: localPath!, atomically: true)
        //let imageData = NSData(contentsOfFile: localPath!)!
        let photoURL = URL.init(fileURLWithPath: localPath!)//NSURL(fileURLWithPath: localPath!)
        print(photoURL)

    }
Run Code Online (Sandbox Code Playgroud)