Skip to content

Instantly share code, notes, and snippets.

@paulhimes
Last active October 11, 2017 14:58
Show Gist options
  • Select an option

  • Save paulhimes/b8713f7024f4e38632aec177e0f657e5 to your computer and use it in GitHub Desktop.

Select an option

Save paulhimes/b8713f7024f4e38632aec177e0f657e5 to your computer and use it in GitHub Desktop.
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