How can I get the GPS coordinates of a UIImage after the picture was taken in a UIImagePickerController? Does the image contain the coordinates in it's metadata or should I create a class for the image and its coordinates and save that?
Thank you.
Please try to look at this and do as said in that answer..
I had done that and got success..
Hope It works for you..
Actually, you can access the metadata of an image right after you take the picture. The protocol method that is called by the system after an image has been selected is,
(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
The NSDictionary argument, info, contains a key: UIImagePickerControllerMediaMetadata
AFAIK, You will need CLLocationManager to get the current location and geotag it.
From iOS 8 onwards you can use Photos Framework.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let url = info[UIImagePickerControllerReferenceURL] as? URL
if url != nil {
let fetchResult = PHAsset.fetchAssets(withALAssetURLs: [url!], options: nil)
let asset = fetchResult.firstObject
print(asset?.location?.coordinate.latitude)
print(asset?.creationDate)
}
}
Related
I want the user to upload an image from the photos library along with more input so I can write it to my database.
I made an object with the received data from the user and I uploaded it (using ".child()") to firebase realtime database, but then I had to add the photo to the object so I used the UIImagePicker to upload the photo then I got stuck.
my problem is that i don't know what type of info I should get from the image so I can add it to the object.
I'm not sure if using an object is the right choice but since the data I’m adding is related to a specific item I thought it would be suitable.
// The object
let object: [String : Any] = ["areaname": areaName! as Any ,"spotNo": spotNo, "loactionLat": areaLat, "locationLong": areaLong]
database.child("Areas").child("Area_\(Int.random(in: 0..<100))" ).setValue(object)
#IBAction func chooseImageButton() {
print("Add image button was pressed")
let vc = UIImagePickerController()
vc.sourceType = .photoLibrary
vc.delegate = self
vc.allowsEditing = true
present(vc, animated: true)
}
extension AddAreaViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediawithInfo info: [UIImagePickerController.InfoKey : Any]) {
print("\(info)")
if let image = info[UIImagePickerController.InfoKey(rawValue: "UIImagePickerControllerEditedImage")] as? UIImage {
}
picker.dismiss(animated: true, completion: nil) }
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
}
Firebase structure
My solution is saving the image using FirebaseStorage. And turn the image url to string write into database. When you need loading image. You can search by that urlstring.
I think #Evan Lu is correct.
You don't need to store the image on firebase database or realtime databse.
You can store the image on FirebaseStorage and save the url on database.
I am attempting to grab an NSURL from an image or video when a user selects it through a imagepickercontroller, using the Photos Framework
import Photos
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let url: NSURL = (info["UIImagePickerControllerReferenceURL"] as! NSURL)
mediaUploadView.image = info[UIImagePickerControllerOriginalImage]as? UIImage
print(url)
self.dismissViewControllerAnimated(true, completion: nil)
return UIImagePickerControllerReferenceURL
}
I receive the error "unexpected non void return value in void function".
The end goal is to use the NSURL to upload the image / video / gif to a Firebase Database.
I'm honestly not sure if my logic here is correct, I'm still new to coding. Any help is greatly appreciated!
In swift, you have to say in the at the end of the function before the opening "{" what the return value is going to be, if you don't, then it is considered a void function. A void function means that it does not return any value. So, to tell it that it will return a NSURL, try this:
import Photos
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) -> NSURL {
let url: NSURL = (info[UIImagePickerControllerReferenceURL] as! NSURL)
mediaUploadView.image = info[UIImagePickerControllerOriginalImage]as? UIImage
print(url)
self.dismissViewControllerAnimated(true, completion: nil)
return url
}
Just copy and paste this, the only difference is that it tells the function what the return value will be, and will no longer return that error. I also suggest removing the "" around the
(info[UIImagePickerControllerReferenceURL] as! NSURL)
This will return a NSURL.
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
imgProfileIcon.image=info[UIImagePickerControllerOriginalImage] as? UIImage
self.dismissViewControllerAnimated(true, completion: nil)
print(info)
if let _ = imagePicked
{
if imagePicked.isEqualToData(UIImagePNGRepresentation(info[UIImagePickerControllerOriginalImage] as! UIImage)!)
{
print("U Picked The Same Image")
}
else
{
print("Different Image Picked")
}
}
imagePicked=UIImagePNGRepresentation(info[UIImagePickerControllerOriginalImage] as! UIImage)
isImagePicked=true
}
This is the delegate function that I use to select an image from The photoLibrary and my objective is to ensure that I don't hit a web service if an user selects the same image..... So while I was trying to compare images it always prints "Different Image Picked" Even though I pick the same image from the photo library.
I've tried all possibilities....even with .equal() but it shows the same... I think that some additional data is added due to which the comparison fails.
Can someone help me out?
I am trying to take a picture with the camera and then detect the faces in it. But it doesn't work... The results array returns a count of zero. I have tested this code with a picture of somebody from the internet and it returned 1 found face. Here is my code:
// MARK: - UIImagePickerControllerDelegate Methods
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
Idea.CurrentIdea.idea.mockups.append(PFFile(data: UIImageJPEGRepresentation(pickedImage, 0.5)!))
//Face Detection
let cid:CIDetector = CIDetector(ofType:CIDetectorTypeFace, context:nil, options:[CIDetectorAccuracy: CIDetectorAccuracyHigh]);
let cii = CIImage(CGImage: pickedImage.CGImage!)
let results:NSArray = cid.featuresInImage(cii)
print(results.count)
for r in results {
let face:CIFaceFeature = r as! CIFaceFeature;
NSLog("Face found at (%f,%f) of dimensions %fx%f", face.bounds.origin.x, face.bounds.origin.y, face.bounds.width, face.bounds.height);
}
}
dismissViewControllerAnimated(true, completion: nil)
}
Any ideas? Thanks! There isn't much on the web about it recently.
Make sure that your image orientation and the expected image orientation for the detector are the same. See this answer for more detail: https://stackoverflow.com/a/17019107/919790
How can I get an URL from UIImage? I've got an image from iPhone library and I resize it and I don't want to save the new image but I want to get it's URL.
You say you don't want to save it, but if you need a URL for a UIImage, you can easily save a temp copy to get the URL that you need.
For newer swift 5:
// Create a URL in the /tmp directory
guard let imageURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("TempImage.png") else {
return
}
let pngData = image.pngData();
do {
try pngData?.write(to: imageURL);
} catch { }
Now you can use the URL to share the image to social media or whatever you need to do with it.
For previous Swift versions:
// Create a URL in the /tmp directory
guard let imageURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("TempImage.png") else {
return
}
// save image to URL
do {
try UIImagePNGRepresentation(myImage)?.write(to: imageURL)
} catch { }
See here for more.
UIImage has no URL or file information. It's just a collection of bytes representing the pixel color data. If you get the UIImage from a UIImagePicker then you can get the URL to the original image in the asset library. But you must get this URL in the image picker delegate method and you must keep track of it separately from the UIImage object.
Edit - based on the OP's comment, this is not the information being requested.
What you are looking for is the UIImagePickerControllerReferenceURL parameter provided by the delegate callback in the info dictionary. What you need to do is:
NSURL* imageURL = [info objectForKey:UIImagePickerControllerReferenceURL];
And it would look something like this:
assets-library://asset/asset.JPG?id=SOME_GUUID&ext=JPG
You could use this latter to refer back to the same image you got in the UIImage* in the callback.
You could subclass UIImage and create an override initWithURL: to save the URL to a created property... something like this.
+ (id) initWithURL:(NSURL*)url
{
[super initWithURL:url];
self.url = url;
}