Save geolocation photo to Swift 3 Photo Library

How to save a photo in a photo library with geolocation metadata?

I asked (and allowed) the application to access the user's location:

private func allowAccessToUserLocation() {

        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
    }

Do I need to request specific offers for the camera application?

EDIT:

I use UIImagePickerController to take photos from the application. All photos taken from the application are stored in the photo library WITHOUT geolocation.

Does this have anything to do with how I can save the image in the photo library? Is my problem here, not the permissions of the main location?

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage{
            UIImageWriteToSavedPhotosAlbum(pickedImage, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)

            dismiss(animated: true, completion: {

                self.showPhoto()})
        }
    }
+4
source share
2

, , , func addAsset(image: UIImage, location: CLLocation? = nil) {...}

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

:

 let locationManager = CLLocationManager()

        if CLLocationManager.locationServicesEnabled() {
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
            locationManager.startUpdatingLocation()

        }
        else{
            //Location service disabled"
        }
        //and then call....

        addAsset(image: pickedImage, location: locationManager.location)

:

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

i :

 locationManager.stopUpdatingLocation()

+1

iOS 8 Apple .

PHAsset :

func addAsset(image: UIImage, location: CLLocation? = nil) {
    PHPhotoLibrary.shared().performChanges({
        // Request creating an asset from the image.
        let creationRequest = PHAssetChangeRequest.creationRequestForAsset(from: image)
        // Set metadata location
        if let location = location {
            creationRequest.location = location
        }
    }, completionHandler: { success, error in
        if !success { NSLog("error creating asset: \(error)") }
    })
}

,

PHPhotoLibrary.requestAuthorization(_:)

:

info[UIImagePickerControllerMediaMetadata]

, , NOT . , CoreLocation.

+3

Source: https://habr.com/ru/post/1670691/


All Articles