Skip to content

Instantly share code, notes, and snippets.

@rnystrom
Created November 3, 2014 16:37
Show Gist options
  • Save rnystrom/691a2303ea26a9e37c8a to your computer and use it in GitHub Desktop.
Save rnystrom/691a2303ea26a9e37c8a to your computer and use it in GitHub Desktop.
A friendly challenge to anyone w/ functional chops to improve this function. No holds barred: op overloading, enum/struct/class, everything-is-a-singleton... go for it! The only rule is no forced unwrapping (e.g. !).
import Foundation
import ImageIO
func datesFromImagesInDir(dir: String) -> [NSDate] {
let fm = NSFileManager.defaultManager()
var error: NSError?
let df = NSDateFormatter()
df.dateFormat = "yyyy:MM:dd HH:mm:ss"
typealias rn_CGPropDictionary = Dictionary<String, AnyObject>
var dates = [NSDate]()
let options = [(kCGImageSourceShouldCache as String): false]
if let imageURLs = fm.contentsOfDirectoryAtPath(dir, error: &error) as? [String] {
for fileName in imageURLs {
if let url = NSURL(fileURLWithPath: dir)?.URLByAppendingPathComponent(fileName) {
if let imageSource = CGImageSourceCreateWithURL(url, nil) {
if let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, options) as? rn_CGPropDictionary {
if let exif = imageProperties[kCGImagePropertyExifDictionary] as? rn_CGPropDictionary {
if let originalDateString = exif[kCGImagePropertyExifDateTimeOriginal] as? String {
if let date = df.dateFromString(originalDateString) {
dates.append(date)
}
}
}
}
}
}
}
}
return dates
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment