Is there any way to get photo location information from a picked photo using the new Swift PhotosPicker?
SwiftUI PhotosPicker Photo Location
Yes, once you have loaded the PhotosPickerItem as some transferable type (and assuming the underlying asset actually has location metadata), you can utilize a framework like CoreImage to read the location metadata:
// `url` here came from the PhotosPickerItem, which was loaded as a Transferable type that had a FileRepresentation.
let image = CIImage(contentsOf: url)!
let properties = image.properties
if let gps = properties[kCGImagePropertyGPSDictionary as String] as? [String: Any] {
let lat = gps[kCGImagePropertyGPSLatitude as String] as! Double
let lon = gps[kCGImagePropertyGPSLongitude as String] as! Double
print(lat, lon)
}
if let uiImage = UIImage(data: data) {
pictureCaptionViewModel.inputImage = uiImage
let image = CIImage(cgImage: uiImage.cgImage!)
let properties = image.properties
if let gps = properties[kCGImagePropertyGPSDictionary as String] as? [String: Any] {
let lat = gps[kCGImagePropertyGPSLatitude as String] as! Double
let lon = gps[kCGImagePropertyGPSLongitude as String] as! Double
print(lat, lon)
}
}
}
I was not able to get the the lat lon. I loaded the image(with location) using PhotoPicker.