本文介绍了获取从UIImagePickerController中选择的UIImage的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to provide a user selection of an image from Photo Library. Thus I'm using UIImagePickerController for selecting. But here comes an issue with getting its initial URL in the file system (I need this for 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)

}

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

imageURL is kinda cryptic. It describes Asset URL, but not one in File System. And it looks like: assets-library://asset/asset.JPG?id=B6C0A21C-07C3-493D-8B44-3BA4C9981C25&ext=JPG.

根据这个逻辑,路径 /asset.JPG 其中 asset.JPG 是图像的名称。

According to this logic the path is /asset.JPG where asset.JPG is a name of the image.

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

Then I'm accessing my Documents folder and trying to find there a file with path:

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

Cryptic as well but seems to be a real path for a not existing image... No image with that path can be found. And this makes me sick.

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

Do I have to save an image from the beginning? Or is there any other way to go?

我使用 AssetsLibrary API查看了几个教程,但我找不到任何有用的方法来解决我的问题。提前谢谢!

I've looked through several tutorials using AssetsLibrary API but I didn't find anything useful to solve my problem. Thank you in advance!

推荐答案

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

Okay, I've solved the issue.

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

All you have to do is simply grab the image (info[UIImagePickerControllerOriginalImage] as UIImage) and save to the given directory. If you need to save only 1 picture it works great. The code id below.

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)

}

这篇关于获取从UIImagePickerController中选择的UIImage的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 13:42