In Android there is a way to take the selected image path and display it in our app. Is there any way to do the same thing in iOS too? To select the image from the picker view, take the image path and display in my app?
Delegate callback didFinishPickingMediaWithInfo. Simply obtain the path from the info dictionary's UIImagePickerControllerReferenceURL.
more check apple documentation
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!)
{
self.displayImg.image = image
let imgURL: NSURL = editingInfo.valueForKey("UIImagePickerControllerReferenceURL") as! NSURL
print(imgURL.absoluteString)
self.dismissViewControllerAnimated(true, completion: nil)
}
UIImagePickerControllerOriginalImage returns only the image's data, with no reference to its local storage.
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
if let localUrl = (info[UIImagePickerControllerMediaURL] ?? info[UIImagePickerControllerReferenceURL]) as? NSURL {
print (localUrl)
//if you want to get the image name
let imageName = localUrl.path!.lastPathComponent
}
}
for more help see this link
Related
Kindly let me know the way to get the path of the image selected using the picker view.
func addImage(sender: AnyObject){
picController.allowsEditing = false
picController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.presentViewController(picController, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
self.dismissViewControllerAnimated(true, completion: nil)
titleImageView.image = image
//Get the path of the image selected
// print(path)
}
I would like to print the path of the image.
Thank you
Working code
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){
self.titleImageView.image = image
let url: NSURL = editingInfo.valueForKey("UIImagePickerControllerReferenceURL") as! NSURL
print(url.absoluteString)
self.dismissViewControllerAnimated(true, completion: nil)
}
The UIImagePickerController API you're using provides a reference to a UIImage object via delegation. With this UIImage reference you can write the image to file and use that path. In order to obtain a reference to the image asset in the library, you'll need to use a different delegate callback: didFinishPickingMediaWithInfo. Simply obtain the path from the info dictionary's UIImagePickerControllerReferenceURL.
For more information, see Apple's documentation.
Can we load images found locally on the device with SDWebimage using URL option? if yes then Im trying to use SDWebimage to display images, below is my code and i am unable to load image in the imageview. its still blank. kindly help me to understand what im doing wrong?
func addImage(sender: AnyObject){
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){
picController.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum;
picController.allowsEditing = true
self.presentViewController(picController, animated: true, completion: nil)
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let url = info[UIImagePickerControllerReferenceURL] as! NSURL
titleImageView.sd_setImageWithURL(url)
self.dismissViewControllerAnimated(true, completion: nil)
}
Note that the UIImagePickerController delegate
imagePickerController: didFinishPickingMediaWithInfo
will have the info as described in documentation-
A dictionary containing the original image and the edited image, if an
image was picked; or a filesystem URL for the movie, if a movie was
picked. The dictionary also contains any relevant editing information.
The keys for this dictionary are listed in Editing Information Keys.
so if it is image that you are looking at, it will be right there in the dictionary itself.
You can get original image directly-
let image = info[UIImagePickerControllerOriginalImage]
if let image = image {
//Set this image to your image view directly
titleImageView.image = image
}
Here info as a NSDictonary and get that key is UIImagePickerControllerOriginalImage
see this code :
func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info:NSDictionary!) {
var tempImage:UIImage = info[UIImagePickerControllerOriginalImage] as UIImage
imagePreview.image = tempImage
self.dismissModalViewControllerAnimated(true)
}
I am using either the camera or the library app to take/select an image
eg.
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
// User has selected an image
// To dismiss the image picker
self.dismissViewControllerAnimated(true, completion: nil)
// Select the Image of interest
// retrieve the object via key from documentation
self.imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
}
If I set a breakpoint at self.imageView.image and step down, I can get the image properties but I don't know how to get the filename.
I'm currently working on a photo app written in Swift. I'm having a problem getting the EXIF data from the photos. I found some tutorials here but couldn't find one in Swift that worked for me. Here is my code:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
let image = info[UIImagePickerControllerOriginalImage] as UIImage
let imageData = UIImageJPEGRepresentation(image, 1.0)
//Assign image to a variable
selectedImage = image
self.dismissViewControllerAnimated(true, completion: nil)
println("finished")
}
How can I get the EXIF data (specifically, the GPS data) from the UIImage? Thanks!
In obj-c with using image picker, I could get path of image with this:
NSURL* localUrl = (NSURL *)[info valueForKey:UIImagePickerControllerReferenceURL];
but when I try this code in swift :
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){
self.dismissViewControllerAnimated(true, completion: { () -> Void in
})
var path : NSURL = editingInfo.valueForKeyPath("UIImagePickerControllerOriginalImage") as NSURL
... }
it crashes and say
fatal error: unexpectedly found nil while unwrapping an Optional value
so, how can I get path of an image in swift?
you lost this line?
imagePicker.allowsEditing = true
if you set
imagePicker.allowsEditing = false
editingInfo will always be nil
hi i got the image url using below code .It works fine in swift 2.0
let path = editingInfo[UIImagePickerControllerReferenceURL] as! NSURL
You are perhaps using a different dictionary? Above it is info, later you are using editingInfo. Explain how you extract your dictionaries with complete code.