Skip to content

Instantly share code, notes, and snippets.

@TheCodedSelf
Created April 29, 2017 06:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheCodedSelf/7ff3a4fb64f8f6131925fa3e6e21efbe to your computer and use it in GitHub Desktop.
Save TheCodedSelf/7ff3a4fb64f8f6131925fa3e6e21efbe to your computer and use it in GitHub Desktop.
Store, retrieve, and delete images in your iOS app's local storage
import UIKit
struct ImageStore {
static func delete(imageNamed name: String) {
guard let imagePath = ImageStore.path(for: name) else {
return
}
try? FileManager.default.removeItem(at: imagePath)
}
static func retrieve(imageNamed name: String) -> UIImage? {
guard let imagePath = ImageStore.path(for: name) else {
return nil
}
return UIImage(contentsOfFile: imagePath.path)
}
static func store(image: UIImage, name: String) throws {
guard let imageData = UIImagePNGRepresentation(image) else {
throw NSError(domain: "com.thecodedself.imagestore", code: 0, userInfo: [NSLocalizedDescriptionKey: "The image could not be created"])
}
guard let imagePath = ImageStore.path(for: name) else {
throw NSError(domain: "com.thecodedself.imagestore", code: 0, userInfo: [NSLocalizedDescriptionKey: "The image path could not be retrieved"])
}
try imageData.write(to: imagePath)
}
private static func path(for imageName: String, fileExtension: String = "png") -> URL? {
let directory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
return directory?.appendingPathComponent("\(imageName).\(fileExtension)")
}
}
@tariqul000
Copy link

here in my local file path but this not showing in ImageView
file:///var/mobile/Containers/Data/Application/8421B8A7-CB60-462B-9DC1-3C0025828CE2/Documents/Fetish.png

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment