I\'ve been learning swift rather quickly, and I\'m trying to develop an OS X application that downloads images.
I\'ve been able to parse the JSON I\'m looking for into a
just change filename.png
to something else
func writeImageToDocs(image:UIImage){
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let destinationPath = URL(fileURLWithPath: documentsPath).appendingPathComponent("filename.png")
debugPrint("destination path is",destinationPath)
do {
try image.pngData()?.write(to: destinationPath)
} catch {
debugPrint("writing file error", error)
}
}
func readImageFromDocs()->UIImage?{
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let filePath = URL(fileURLWithPath: documentsPath).appendingPathComponent("filename.png").path
if FileManager.default.fileExists(atPath: filePath) {
return UIImage(contentsOfFile: filePath)
} else {
return nil
}
}