Created
July 4, 2025 11:34
-
-
Save jacobsapps/2edfd12c8785711d3de7231f53be9672 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
| enum ImageFileManager { | |
| enum ImageCompressionType { | |
| case jpeg(quality: CGFloat) | |
| case png | |
| } | |
| static func saveImage(_ image: UIImage, key: String, compression: ImageCompressionType) { | |
| guard let data = switch compression { | |
| case .jpeg(let quality): | |
| image.jpegData(compressionQuality: quality) | |
| case .png: | |
| image.pngData() | |
| }, let folder = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return } | |
| let fileURL = folder.appendingPathComponent(key) | |
| try? data.write(to: fileURL, options: .atomic) | |
| } | |
| static func path(for key: String) -> URL? { | |
| guard let folder = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return nil } | |
| return folder.appendingPathComponent(key) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment