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

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.

Related

UImage? isn't convertible to UIImage: Xcode throws error when trying to use original image

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
// The info dictionary may contain multiple representations of the image. You want to use the original.
guard let selectedImage = info[.originalImage] as?
UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
// Set photoImageView to display the selected image.
photoImageView.image = selectedImage
// Dismiss the picker.
dismiss(animated: true, completion: nil)
}
I am trying to build an iOS app that allows you to pick an image from the library, but it keeps failing with the error "UIImage? is not convertible to UIImage. Is there a way to fix this error or bypass it as of now? I am running Xcode 10.1 on MacOS 10.14.
Replace .originalImage with UIImagePickerControllerOriginalImage
This worked for me.
Xcode version: 10.1
Swift version: 4.0
info[.originalImage] is a Any? value. Si it is like it can contain a UIImage? value or nil. So, try:
guard guard let selectedImage = info[.originalImage] as? UIImage? else { ... }
adding that question mark to UIImage. That's all.

How to get gif file location from UIImagePickerController in Objective C? [duplicate]

In iOS Swift I'm having difficulty loading an animated GIF from the device photo library to a UIImageView or creating a .gif file from it. I have no problem displaying the animated GIF if it's a file on a server or if it's right in the app. However, when I load it from the photo library, I lose the animation. I found some Objective-C solutions and tried to convert them over to Swift, but haven't had any luck.
Here is what I have so far:
#IBAction func buttonTapped(sender: UIButton) {
selectPhoto()
}
func selectPhoto() {
let photoLibraryController = UIImagePickerController()
photoLibraryController.delegate = self
photoLibraryController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
let mediaTypes:[String] = [kUTTypeImage as String]
photoLibraryController.mediaTypes = mediaTypes
photoLibraryController.allowsEditing = false
self.presentViewController(photoLibraryController, animated: true, completion: nil)
}
// UIImagePickerControllerDelegate
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let origImage = info[UIImagePickerControllerOriginalImage] as! UIImage
self.imageView.image = origImage
picker.dismissViewControllerAnimated(true, completion: nil)
}
Optimally, I would like to save the photo library image to a gif file on the server. From there I have no trouble displaying it. However, I need some help getting the data from the photo library into some type of property, without losing the animation. Do I need to use something like ALAssetsLibrary for this?
Thanks.
Yes, Use ALAssetsLibrary → now called PHAsset.
You should get the NSData of the gif, not UIImage( because UIImage will only get the first frame.)
So basically you would do something like this:
One you get the asset
let requestOptions = PHImageRequestOptions()
requestOptions.isSynchronous = true // adjust the parameters as you wish
PHImageManager.default().requestImageData(for: asset, options: requestOptions, resultHandler: { (imageData, UTI, _, _) in
if let uti = UTI,let data = imageData ,
// you can also use UTI to make sure it's a gif
UTTypeConformsTo(uti as CFString, kUTTypeGIF) {
// save data here
}
})
Resource: PHAsset
You can get an access to the location of the image file using InfoKey .imageURL and retrieve Data from this URL
// UIImagePickerControllerDelegate
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let path = info[.imageURL] as? URL {
if let data = try? Data(contentsOf: path) {
self.imageView.image = UIImage(data: data)
//Now you have Data of a file: so you can convert it to image or gif or send to server or start animation (e.g. Framework JellyGIF)
self.imageView.startGif(with: .data(data))
}
picker.dismissViewControllerAnimated(true, completion: nil)
}

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 get selected image path

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.

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

Resources