-
-
Save algal/01caaaa6b0d5f2a937c7 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
import Foundation | |
import ImageIO | |
// | |
// before | |
// | |
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 | |
} | |
// | |
// after | |
// | |
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 | |
} | |
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] | |
let dateTimeOriginal = { (exif: rn_CGPropDictionary) in (exif[kCGImagePropertyExifDateTimeOriginal] as? String) } | |
let exif = { (imageProperties: rn_CGPropDictionary) in imageProperties[kCGImagePropertyExifDictionary] as? rn_CGPropDictionary } | |
let imageProperties = { CGImageSourceCopyPropertiesAtIndex($0, 0, options) as? rn_CGPropDictionary } | |
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