Last active
October 14, 2023 22:50
-
-
Save nitrag/343fe13f01bb0ef3692f2ae2dfe33e86 to your computer and use it in GitHub Desktop.
Generate Metadata Exif for GPS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Generate EXIF GPS metadata | |
// Swift 3 | |
// Exif Version 2.2.0.0 supports decimal degrees | |
import Foundation | |
import CoreLocation | |
import ImageIO | |
extension CLLocation { | |
func exifMetadata(heading: CLHeading? = nil) -> NSMutableDictionary { | |
let GPSMetadata = NSMutableDictionary() | |
let altitudeRef = Int(self.altitude < 0.0 ? 1 : 0) | |
let latitudeRef = self.coordinate.latitude < 0.0 ? "S" : "N" | |
let longitudeRef = self.coordinate.longitude < 0.0 ? "W" : "E" | |
// GPS metadata | |
GPSMetadata[(kCGImagePropertyGPSLatitude as String)] = abs(self.coordinate.latitude) | |
GPSMetadata[(kCGImagePropertyGPSLongitude as String)] = abs(self.coordinate.longitude) | |
GPSMetadata[(kCGImagePropertyGPSLatitudeRef as String)] = latitudeRef | |
GPSMetadata[(kCGImagePropertyGPSLongitudeRef as String)] = longitudeRef | |
GPSMetadata[(kCGImagePropertyGPSAltitude as String)] = Int(abs(self.altitude)) | |
GPSMetadata[(kCGImagePropertyGPSAltitudeRef as String)] = altitudeRef | |
GPSMetadata[(kCGImagePropertyGPSTimeStamp as String)] = self.timestamp.isoTime() | |
GPSMetadata[(kCGImagePropertyGPSDateStamp as String)] = self.timestamp.isoDate() | |
GPSMetadata[(kCGImagePropertyGPSVersion as String)] = "2.2.0.0" | |
if let heading = heading { | |
GPSMetadata[(kCGImagePropertyGPSImgDirection as String)] = heading.trueHeading | |
GPSMetadata[(kCGImagePropertyGPSImgDirectionRef as String)] = "T" | |
} | |
return GPSMetadata | |
} | |
} | |
extension Date { | |
func isoDate() -> String { | |
let f = DateFormatter() | |
f.timeZone = TimeZone(abbreviation: "UTC") | |
f.dateFormat = "yyyy:MM:dd" | |
return f.string(from: self) | |
} | |
func isoTime() -> String { | |
let f = DateFormatter() | |
f.timeZone = TimeZone(abbreviation: "UTC") | |
f.dateFormat = "HH:mm:ss.SSSSSS" | |
return f.string(from: self) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://stackoverflow.com/questions/52810822/saving-exif-data-to-jpeg-swift