我已经试了几天,想办法从照片库内的照片中获取GPS位置。我正在使用UIImagePicker来获取照片,但是在互联网上发布的解决方案似乎没有一个有效。我知道我应该把UIImage转换成PHAsset,但是在任何地方人们都在使用一个被弃用的方法fetchAssets(withALAssetURLs: [URL], options: PHFetchOptions?)
。有什么办法可以做到这一点吗?非常感谢你
最佳答案
我希望这对你有帮助:-
//step1:- import Photos
//step2:- when you presenting imagepicker controller
if PHPhotoLibrary.authorizationStatus() == .authorized || PHPhotoLibrary.authorizationStatus() == .authorized{
PHPhotoLibrary.requestAuthorization { [weak self](_) in
// Present the UIImagePickerController
self?.present(self!.imagePicker, animated: true, completion: nil)
}
}
迅捷3.0和迅捷4.0
//step3:-
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
//obtaining saving path
let fileManager = FileManager.default
let documentsPath = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first
let imagePath = documentsPath?.appendingPathComponent("image.jpg")
// extract image from the picker and save it
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
let data = UIImageJPEGRepresentation(pickedImage, 0.75)
data.write(toFile: localPath!, atomically: true)
}
let coordinate = (info[UIImagePickerControllerPHAsset] as? PHAsset)?.location?.coordinate
print(coordinate?.latitude ?? "No latitude found")
print(coordinate?.longitude ?? "No longitude found")
self.dismiss(animated: true, completion: nil)
}
银行代码4.2
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
//obtaining saving path
let fileManager = FileManager.default
let documentsPath = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first
let imagePath = documentsPath?.appendingPathComponent("image.jpg")
// extract image from the picker and save it
if let pickedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
let imageData = pickedImage.jpegData(compressionQuality: 0.75)
try! imageData?.write(to: imagePath!)
}
let coordinate = (info[UIImagePickerController.InfoKey.phAsset] as? PHAsset)?.location?.coordinate
print(coordinate?.latitude ?? "No latitude found")
print(coordinate?.longitude ?? "No longitude found")
self.dismiss(animated: true, completion: nil)
}
谢谢
关于ios - 如何从照片获取GPS数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52997463/