Last active
October 11, 2017 14:58
-
-
Save paulhimes/b8713f7024f4e38632aec177e0f657e5 to your computer and use it in GitHub Desktop.
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
| extension DateFormatter { | |
| static var exifDateFormatter: DateFormatter = { | |
| let formatter = DateFormatter() | |
| formatter.dateFormat = "yyyy':'MM':'dd' 'HH':'mm':'ss'.'SSS" | |
| formatter.locale = Locale(identifier: "en_US_POSIX") | |
| formatter.timeZone = TimeZone.current | |
| return formatter | |
| }() | |
| } | |
| func getDateFromImageData(_ data: Data) -> Date? { | |
| let exif = getEXIFFromImageData(data) | |
| if let exifDictionary = exif["{Exif}"] as? [String: Any], | |
| let dateTimeOriginal = exifDictionary["DateTimeOriginal"] as? String, | |
| let subsecondTimeOriginal = exifDictionary["SubsecTimeOriginal"] as? String, | |
| let date = DateFormatter.exifDateFormatter.date(from: "\(dateTimeOriginal).\(subsecondTimeOriginal)") { | |
| return date | |
| } else { | |
| return nil | |
| } | |
| } | |
| func getEXIFFromImageData(_ data: Data) -> NSDictionary { | |
| let imageSourceRef = CGImageSourceCreateWithData(data as CFData, nil); | |
| let currentProperties = CGImageSourceCopyPropertiesAtIndex(imageSourceRef!, 0, nil) | |
| let mutableDict = NSDictionary(dictionary: currentProperties!) | |
| return mutableDict | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment