Reading the GPS data from the image returned by the camera in iOS iphone

后端 未结 9 1361
余生分开走
余生分开走 2020-11-29 21:36

I need to get the GPS coordinates of an image taken with the iOS device\'s camera. I do not care about the Camera Roll images, just the image taken with UIImagePickerControl

9条回答
  •  广开言路
    2020-11-29 22:11

    Swift answer:

    import AssetsLibrary
    import CoreLocation
    
    
    // MARK: - UIImagePickerControllerDelegate
    extension ViewController: UIImagePickerControllerDelegate {
        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
            defer {
                dismiss(animated: true, completion: nil)
            }
            guard picker.sourceType == .photoLibrary else {
                return
            }
            guard let url = info[UIImagePickerControllerReferenceURL] as? URL else {
                return
            }
    
            let library = ALAssetsLibrary()
            library.asset(for: url, resultBlock: { (asset) in
                guard let coordinate = asset?.value(forProperty: ALAssetPropertyLocation) as? CLLocation else {
                    return
                }
                print("\(coordinate)")
    
                // Getting human-readable address.
                let geocoder = CLGeocoder()
                geocoder.reverseGeocodeLocation(coordinate, completionHandler: { (placemarks, error) in
                    guard let placemark = placemarks?.first else {
                        return
                    }
                    print("\(placemark.addressDictionary)")
                })
            }, failureBlock: { (error: Error?) in
                print("Unable to read metadata: \(error)")
            })
        }
    }
    

提交回复
热议问题