Skip to content

Instantly share code, notes, and snippets.

@laevandus
Last active October 23, 2018 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laevandus/28acb7f2c3a2728c2460271bf2cc8eb9 to your computer and use it in GitHub Desktop.
Save laevandus/28acb7f2c3a2728c2460271bf2cc8eb9 to your computer and use it in GitHub Desktop.
extension PersistentDataStore {
func loadImage(forIdentifier identifier: Identifier, completionHandler block: @escaping (UIImage?) -> (Void)) {
loadData(forIdentifier: identifier, dataTransformer: { UIImage(data: $0) }, completionHandler: block)
}
func storeImage(_ image: UIImage, identifier: String = UUID().uuidString, completionHandler handler: @escaping (Result) -> ()) {
storeData({ image.jpegData(compressionQuality: 1.0) }, identifier: identifier, completionHandler: handler)
}
}
// Examples:
persistentStore.storeImage(image) { (result) in
print(result)
}
persistentStore.loadImage(forIdentifier: "my_identifier") { (image) -> (Void) in
guard let image = image else {
print("Failed loading image.")
return
}
print(image)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment