Ios 从UIImagePickerController获取图像位置

Ios 从UIImagePickerController获取图像位置,ios,objective-c,swift,uiimagepickercontroller,photosframework,Ios,Objective C,Swift,Uiimagepickercontroller,Photosframework,我尝试了多种方法来获取通过UIImagePickerController相机拍摄的图像的位置 我想实现的是,我想使用UIImagePickerControllerCamera选择一个图像,我必须将它保存到照片库中,这样只有我才能从中取回相位集以及与之相关的位置 //MARK: Saving an Image to PhotoLibrary and taking back the PHAsset class func savingThis(image : UIImage, comp

我尝试了多种方法来获取通过
UIImagePickerController相机拍摄的图像的位置

我想实现的是,我想使用
UIImagePickerController
Camera
选择一个图像,我必须将它保存到
照片库中,这样只有我才能从中取回
相位集以及与之相关的位置

   //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)
                }
        })
    }
我已经尝试了这个代码,以保存并取回相位集。但问题是,这个
PHAsset
没有任何与之关联的位置,不知道为什么?我错过了什么

我相信我不必手动将
GPS
数据设置到图像的元数据中,对吗?我认为
Photos框架
assetlibrary
可以解决这个问题。因此,正如您所知,
资产库
已被弃用,我们唯一的选择是使用
照片框架
。我在网上读到,将图像保存到
照片库
会处理它。对不对

还有别的选择吗?我是否应该使用
UIImageWriteToSavedPhotosAlbum
方法将图像保存到
Camera Roll
,并且我可以使用
Photos Framework
收回最近的照片。但我不认为,
UIImageWriteToSavedPhotosAlbum
能解决位置问题


你有什么想法吗?

首先感谢所有关心这个问题的人

我找到了答案

  //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)
                }
        })
    }

首先感谢所有关心这个问题的人

我找到了答案

  //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)
                }
        })
    }

当您收到来自
UIImagePickerController
的回调时:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo 
                               info: [UIImagePickerController.InfoKey : Any]) {}
您可以尝试从信息字典中获取图像位置。 为此,您需要请求相关的
PHAsset
对象:

if let assetImage = info[UIImagePickerController.InfoKey.phAsset] as? PHAsset {
    print("Image location info =  \( assetImage.location))")
}
重要信息-在使用此方法之前,您需要请求用户权限:

PHPhotoLibrary.requestAuthorization()

如果不执行此操作,
UIImagePickerController.InfoKey.phAsset
的信息字典将在您从
UIImagePickerController
收到回调时返回
nil

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo 
                               info: [UIImagePickerController.InfoKey : Any]) {}
您可以尝试从信息字典中获取图像位置。 为此,您需要请求相关的
PHAsset
对象:

if let assetImage = info[UIImagePickerController.InfoKey.phAsset] as? PHAsset {
    print("Image location info =  \( assetImage.location))")
}
重要信息-在使用此方法之前,您需要请求用户权限:

PHPhotoLibrary.requestAuthorization()

如果不执行此操作,
UIImagePickerController.InfoKey.phAsset
的信息字典将返回
nil

是否尝试获取图像路径?我正在使用UIImagePickerController通过在UIImagePickerController中将相机设置为源类型来拍摄新照片。选择图像后,我将图像保存到照片库中,并将其作为图像集取回。相位集没有与其关联的任何位置,我需要相位集的位置。是否尝试获取图像的路径?我正在使用UIImagePickerController通过在UIImagePickerController中将相机设置为源类型来拍摄新照片。选择图像后,我将图像保存到照片库中,并将其作为图像集取回。PHAsset没有任何与之关联的位置,我想要与PHAsset关联的位置。