0
public func setExiffrom(url:URL?){
    if url == nil{
        return
    }

    let imagedata = try! Data.init(contentsOf: url!)
    let source: CGImageSource = CGImageSourceCreateWithData((imagedata as CFData), nil)!
    if let object = CGImageSourceCopyPropertiesAtIndex(source, 0, nil) as? [String: Any]{
        self.imageView.image = UIImage(data: imagedata)
        self.exif = Exif(object)

        self.textView.text = self.exif?.gps?.getFormattedString(valueSeperator: " = ", lineSeperator: "")
    }
}

public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    let image = info[UIImagePickerControllerOriginalImage] as? UIImage
    let url = info[UIImagePickerControllerImageURL] as? URL

    self.imageView.image = image
    self.choosenImage = image

    self.setExiffrom(url: url)
    self.dismiss(animated: true, completion: nil)
}

I would like to incorporate reading the location of a user's photos into an app so I learned about exif and how it is where lat/lon are stored. I had someone help me with the coding and for some images in the app it would parse the exif out of the selected image fine... BUT for most images no location data was present in the exif. I checked in iOS Photos app, and in there the location data was correctly presented for all the images (pushing up on a photo shows it on a map)

I tested on my macbook as well and found something surprising. When I right clicked and clicked "get info" on an image on the Photos app on the macbook it displayed with the location, but as soon as I copied it to my desktop it no longer had the location data. A new security feature perhaps?

My question is how do I write an app that gets the location correctly so may app can help users capture memories? If the phone's Photos app can see the location then that means the data is there, so it is frustrating that whenever the image is copied it appears the location data is stripped. Perhaps I need to somehow read the data BEFORE I call the "let image = info[UIImagePickerControllerOriginalImage] because that is somehow stripping off the location info? If so how would I do that? Maybe there is just a better technique?

-- any help, would be appreciated, I am stumped...

Daniel Patriarca
  • 361
  • 3
  • 20

2 Answers2

1

I'm using PHAsset() for my app, take a look at it. You do not have to examine the exif data to get the location of the image. I think you can use the same property (location.coordinate) also with UIImagePicker .. but I'm not sure

            // request options for PHAsset
            let allPhotosOptions = PHFetchOptions()
            allPhotosOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]

            // get the requested AssetData
            fetchResult = PHAsset.fetchAssets(with: allPhotosOptions)

            // check if we got any
            if fetchResult != nil {

                // loop over result
                for index in 0 ..< fetchResult!.count {

                    // take the next item
                    let item = fetchResult!.object(at: index)

                    // check if it is a photo
                    if item.mediaType == .image {

                        // yes, it is a photo, so check location

                        // check if the location data is valid
                        if item.location != nil {

                            // as an example: create a mapPoint from the coordination
                            let locationPoint = MKMapPointForCoordinate(item.location!.coordinate)

                         } // location not nil
                    } // is an image
               } // loop
          } // result not nil
Hardy_Germany
  • 1,259
  • 13
  • 19
0

In case it helps anyone else I found this thread on the apple developer's forumn... https://forums.developer.apple.com/thread/96160

Daniel Patriarca
  • 361
  • 3
  • 20