获取从UIImagePickerController中select的UIImage的URL

我试图提供一个用户从图片库中select一个图像。 因此我使用UIImagePickerController进行select。 但是,在文件系统中获取其初始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) } 

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

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

然后我正在访问我的文档文件夹,并试图find一个文件的path:

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

神秘,但似乎是一个不存在的图像真正的path…没有图像的path可以find。 这使我感到恶心。

我是否需要从头开始保存图像? 还是还有其他的路要走吗?

我已经浏览了几个使用AssetsLibrary API的教程,但没有find有用的东西来解决我的问题。 先谢谢你!

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

所有你需要做的只是抓住图像( 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) } 
  func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) { //this block of code grabs the path of the file let imageURL = info[UIImagePickerControllerReferenceURL] as NSURL let imagePath = imageURL.path! let localPath = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent(imagePath) //this block of code adds data to the above path let path = localPath.relativePath! let imageName = info[UIImagePickerControllerOriginalImage] as UIImage let data = UIImagePNGRepresentation(imageName) data?.writeToFile(imagePath, atomically: true) //this block grabs the NSURL so you can use it in CKASSET let photoURL = NSURL(fileURLWithPath: path) } 

检查此解决scheme。

  import AssetsLibrary func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { self.imagePicker = picker if let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage { let controller = CropViewController(image: selectedImage ) controller.delegate = self picker.presentViewController(controller, animated: true, completion: nil) } else if let imageUrl = info[UIImagePickerControllerReferenceURL] as? NSURL{ let assetLibrary = ALAssetsLibrary() assetLibrary.assetForURL(imageUrl , resultBlock: { (asset: ALAsset!) -> Void in if let actualAsset = asset as ALAsset? { let assetRep: ALAssetRepresentation = actualAsset.defaultRepresentation() let iref = assetRep.fullResolutionImage().takeUnretainedValue() let image = UIImage(CGImage: iref) let controller = CropViewController(image: image) controller.delegate = self picker.presentViewController(controller, animated: true, completion: nil) } }, failureBlock: { (error) -> Void in }) } }