Last active
July 28, 2016 19:26
-
-
Save malcommac/a3d1f7ab631e5eed5ab235caffdc6de8 to your computer and use it in GitHub Desktop.
Another async function
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
| 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