Skip to content

Instantly share code, notes, and snippets.

@malcommac
Last active July 28, 2016 19:26
Show Gist options
  • Select an option

  • Save malcommac/a3d1f7ab631e5eed5ab235caffdc6de8 to your computer and use it in GitHub Desktop.

Select an option

Save malcommac/a3d1f7ab631e5eed5ab235caffdc6de8 to your computer and use it in GitHub Desktop.
Another async function
func saveToDisk(name:String, image: UIImage, in context: TaskContext) -> Task<NSURL> {
let task = Task<NSURL>(in: context) { (fullfill, reject) -> (Void) in
let filemanager = NSFileManager.defaultManager()
let documentsPath : AnyObject = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask,true).first!
let fPath = documentsPath.stringByAppendingString("/\(name)") as String
if(!filemanager.fileExistsAtPath(fPath)) {
guard let data = UIImagePNGRepresentation(image) else {
reject(Error.Generic(message: "Cannot convert image"))
return
}
do {
try data.writeToFile(fPath as String, options: NSDataWritingOptions.AtomicWrite)
fullfill(NSURL(fileURLWithPath: fPath))
} catch let err as NSError {
reject(Error.Generic(message: err.localizedDescription))
}
} else {
reject(Error.DuplicateFile(path: fPath))
}
}
return task
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment