IOS Swift get selected image path - ios

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.

Related

Swift, How to get the image path from UIPickerView in iOS?

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

IOS Swift load (local)image with URL

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

Selecting image from library doesn't call "didFinishPickingImage" function - Swift

I have a UIImagePickerController with the option to select from library.
When the accessLibrary button is called, it prompts another UIImagePickerController that has source type .PhotoLibrary.
func accessLibrary(sender: UIButton!) {
NSLog("access library")
choseFromLibrary = true
self.libraryPicker = UIImagePickerController()
self.libraryPicker!.allowsEditing = false
self.libraryPicker!.sourceType = .PhotoLibrary
self.presentViewController(self.libraryPicker!, animated: true, completion: nil)
}
This all works well, but when the user does select a photo, I want to pass that image to a view and then show that view on the screen.
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let metadata = info[UIImagePickerControllerMediaMetadata] as? NSDictionary
var image = info[UIImagePickerControllerOriginalImage] as? UIImage
if (image != nil) {
if (choseFromLibrary) {
let cnfrmImgView = ConfirmImageView.init(image: image!)
self.view.addSubview(cnfrmImgView)
}
else {
if (frontFacing) {
image! = UIImage(CGImage: image!.CGImage!, scale: 1.0, orientation: .LeftMirrored)
}
let cnfrmImgView = ConfirmImageView.init(image: image!)
self.view.addSubview(cnfrmImgView)
}
}
}
The above function works if the image is taken using the camera, but not using the library.
When a user picks a photo from the library, the library picker controller just dismisses itself and the view is not display.
I ran an NSLog("here") to check if the didFinishPickingImageWithInfo function is called when choosing from the library and in fact it is not.
didFinishPickingImageWithInfo is a delegate message (UIImagePickerControllerDelegate). The problem, however, is that you have not given your image picker any delegate! Thus, it has no one to send delegate messages to.
if you want to implement a Instagram like image picker than try this
Image Picker like Instagram
The delegate function is updated in new swift versions from 4 to upwards.
Use this function instead
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { }

How can I return a string of a photo filename?

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.

How to get path of image, selected from gallery in swift

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.

Resources