Last active
March 8, 2017 10:55
-
-
Save zntfdr/69543d864527114a6d1bc161b5d2b243 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
extension UIImage { | |
func load(image imageName: String) -> UIImage { | |
// declare image location | |
let imagePath: String = "\(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])/\(imageName).png" | |
let imageUrl: URL = URL(fileURLWithPath: imagePath) | |
// check if the image is stored already | |
if FileManager.default.fileExists(atPath: imagePath), | |
let imageData: Data = try? Data(contentsOf: imageUrl), | |
let image: UIImage = UIImage(data: imageData, scale: UIScreen.main.scale) { | |
return image | |
} | |
// image has not been created yet: create it, store it, return it | |
let newImage: UIImage = // create your UIImage here | |
try? UIImagePNGRepresentation(newImage)?.write(to: imageUrl) | |
return newImage | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am wondering if the load method should be static so that you can call UIImage.load(_:).