user6375148
user6375148

Reputation:

Get Image location from UIImagePickerController

I have tried multiple ways to get the location of an image which took through the UIImagePickerController Camera.

What I want to achieve is that, I want to select an image using UIImagePickerController Camera and I have to save it into Photo Library so that only I can take back the PHAsset from it and also the location associated with it.

   //MARK: Saving an Image to PhotoLibrary and taking back the PHAsset

    class func savingThis(image : UIImage, completion : (asset : PHAsset?) -> ())
    {
        var localIdentifier : String?
        let imageManager = PHPhotoLibrary.sharedPhotoLibrary()

        imageManager.performChanges({ () -> Void in

            let request = PHAssetChangeRequest.creationRequestForAssetFromImage(image)

            if let properAsset = request.placeholderForCreatedAsset {
                localIdentifier = properAsset.localIdentifier
            }
            else {
                completion(asset: nil)
            }

            }, completionHandler: { (success, error) -> Void in

                if let properLocalIdentifier = localIdentifier {

                    let result = PHAsset.fetchAssetsWithLocalIdentifiers([properLocalIdentifier], options: nil)
                    if result.count > 0  {
                        completion(asset: result[0] as? PHAsset)
                    }
                    else {
                        completion(asset: nil)
                    }
                }
                else {
                    completion(asset: nil)
                }
        })
    }

I have tried this code, to save and get back the PHAsset. But the problem is that this PHAsset does not have any location associated with it, wondering why? And what I missed?

I believe that I don't have to manually set GPS data into image's metadata right? I think that Photos Framework or Asset Library takes care of it. So as you know that Asset Library is deprecated our only option is to use Photos Framework. I read online that, Saving image to Photo Library takes care of it. Isn't it correct?

Is there any alternative? Should I use UIImageWriteToSavedPhotosAlbum method to save image to Camera Roll, And I can take back the very recent photo using Photos Framework. But I don't think that UIImageWriteToSavedPhotosAlbum will take care of the location thing.

Do you have any thoughts?

Upvotes: 0

Views: 1813

Answers (2)

When you get a callback from UIImagePickerController:

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

you can try to get the image location from the info dictionary. For that, you need to request the related PHAsset object:

if let assetImage = info[UIImagePickerController.InfoKey.phAsset] as? PHAsset {
    print("Image location info =  \( assetImage.location))")
}

Important - before using this approach you need to request the user permission:

PHPhotoLibrary.requestAuthorization()

If you do not do it the info dictionary for UIImagePickerController.InfoKey.phAsset will return nil.

Upvotes: 0

user6375148
user6375148

Reputation:

First of all thanking all who were all kind to take a look into the question.

I found my answer.

  //MARK: Saving an Image to PhotoLibrary and taking back the PHAsset

    class func savingThis(image : UIImage, completion : (asset : PHAsset?) -> ())
    {
        var localIdentifier : String?
        let imageManager = PHPhotoLibrary.sharedPhotoLibrary()

        imageManager.performChanges({ () -> Void in

            let request = PHAssetChangeRequest.creationRequestForAssetFromImage(image)

            request.location = // Assigned current location here :)

            if let properAsset = request.placeholderForCreatedAsset {
                localIdentifier = properAsset.localIdentifier
            }
            else {
                completion(asset: nil)
            }

            }, completionHandler: { (success, error) -> Void in

                if let properLocalIdentifier = localIdentifier {

                    let result = PHAsset.fetchAssetsWithLocalIdentifiers([properLocalIdentifier], options: nil)

                    if result.count > 0   {
                        completion(asset: let asset = result[0] as? PHAsset)
                    }
                    else {
                        completion(asset: nil)
                    }
                }
                else {
                    completion(asset: nil)
                }
        })
    }

Upvotes: 2

Related Questions