Skip to content

Instantly share code, notes, and snippets.

@kostiakoval
Forked from rnystrom/:(
Last active August 29, 2015 14:08
Show Gist options
  • Save kostiakoval/b882cbb0e556e9a6463f to your computer and use it in GitHub Desktop.
Save kostiakoval/b882cbb0e556e9a6463f to your computer and use it in GitHub Desktop.
import Foundation
import ImageIO
infix operator >>= { associativity left }
func >>=<A,B>(l: A?, r: A -> B?) -> B? {
if let x = l {
return r(x)
}
return nil
}
func flatMap<A,B>(list: [A], f: A -> B?) -> [B] {
var result : [B] = []
for x in list {
if let value = f(x) {
result.append(value)
}
}
return result
}
typealias rn_CGPropDictionary = Dictionary<String, AnyObject>
func string(input: rn_CGPropDictionary, key: String) -> String? {
return input[key] as? String
}
func dictionary(input: rn_CGPropDictionary, key: String) -> rn_CGPropDictionary? {
return input[key] as? rn_CGPropDictionary
}
func datesFromImagesInDir(dir: String) -> [NSDate] {
let fm = NSFileManager.defaultManager()
var error: NSError?
let df = NSDateFormatter()
df.dateFormat = "yyyy:MM:dd HH:mm:ss"
let options = [String(kCGImageSourceShouldCache) : false]
let dateTimeOriginal = { string($0, kCGImagePropertyExifDateTimeOriginal) }
let exif = { dictionary($0, kCGImagePropertyExifDictionary) }
let imageProperties = { CGImageSourceCopyPropertiesAtIndex($0, 0, options) }
let imageSource = { CGImageSourceCreateWithURL($0, nil) }
let absoluteURL = { (fileName: String) in NSURL(fileURLWithPath: dir)?.URLByAppendingPathComponent(fileName) }
let directoryContents = fm.contentsOfDirectoryAtPath(dir, error: &error) as? [String]
return directoryContents.map {
return flatMap($0) { fileName in
absoluteURL(fileName) >>= imageSource >>= imageProperties >>= exif >>= dateTimeOriginal >>= df.dateFromString
}
} ?? []
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment