Retrieving the UIImage URL Selected from the UIImagePickerController

I am trying to provide the user with an image selection from Photo Library. Thus, I use UIImagePickerController to select. But here there is a problem with getting the source URL in the file system (I need this for CKAsset ).

My code.

 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 is cryptic. It describes the URL of the resource, but not one on the file system. And it looks like this: assets-library://asset/asset.JPG?id=B6C0A21C-07C3-493D-8B44-3BA4C9981C25&ext=JPG .

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

Then I get access to the My Documents folder and try to find the file with the outline there:

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

A mysterious, but it seems, real path for a nonexistent image ... No image with this path was found. And it makes me sick.

Do I need to save the image from the very beginning? Or is there another way?

I looked through several tutorials using the AssetsLibrary API, but I did not find anything useful to solve my problem. Thank you in advance!

+6
source share
4 answers

Ok, I solved the problem.

All you have to do is just grab the image ( info[UIImagePickerControllerOriginalImage] as UIImage ) and save it in this directory. If you need to save only 1 image, it works great. 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) } 
+24
source
  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) } 
+3
source

check out this solution.

  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 }) } } 
+1
source

In SWIFT 4 u You can try this, it works correctly.

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

Source: https://habr.com/ru/post/981817/


All Articles