Created
February 3, 2019 08:21
-
-
Save NeilsUltimateLab/60fbc564f42795b3eaa280227b2cb7ad to your computer and use it in GitHub Desktop.
Save images to file system.
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
import UIKit | |
struct FileCoordinator { | |
static func isFileExist(at url: URL, destinationUrl: @escaping (URL?)->Void) { | |
let backgroundQueue = DispatchQueue.global(qos: DispatchQoS.QoSClass.background) | |
backgroundQueue.async { | |
guard let documentUrl = FileManager.default.urls(for: .documentDirectory, in: FileManager.SearchPathDomainMask.userDomainMask).first else { | |
DispatchQueue.main.async { | |
destinationUrl(nil) | |
} | |
return | |
} | |
do { | |
let directoryContent = try FileManager.default.contentsOfDirectory(at: documentUrl, includingPropertiesForKeys: nil, options: []) | |
let path = directoryContent.filter({$0.fileName == url.fileName}).first | |
DispatchQueue.main.async { | |
destinationUrl(path) | |
} | |
} catch { | |
print(error) | |
DispatchQueue.main.async { | |
destinationUrl(nil) | |
} | |
} | |
} | |
} | |
@discardableResult | |
static func removeItem(at url: URL) -> Bool { | |
do { | |
try FileManager.default.removeItem(at: url) | |
print("\nImage deleted successfully at \(url)") | |
return true | |
} catch { | |
print(error) | |
return false | |
} | |
} | |
static func save(image: UIImage, inFolder folderName: String, with referenceUrl: URL? = nil) -> (folderPath: URL?, fileURL: URL?) { | |
let timeStamp = Date.timeIntervalSinceReferenceDate | |
let timeStampString = "\(Int(timeStamp))" | |
let fileName = (timeStampString).appending(".").appending(referenceUrl?.fileExtension ?? "JPG") | |
do { | |
let imageDirectory = try FileManager.default.url(for: .documentDirectory, | |
in: .userDomainMask, | |
appropriateFor: nil, | |
create: true) | |
let newDir = imageDirectory.appendingPathComponent(folderName, isDirectory: true) | |
let writePath = newDir.appendingPathComponent(fileName) | |
try? FileManager.default.createDirectory(at: newDir, withIntermediateDirectories: true, attributes: nil) | |
let data = UIImageJPEGRepresentation(image, 0.3) | |
do { | |
try data?.write(to: writePath, options: Data.WritingOptions.atomicWrite) | |
print("\nImage file successfully saved to: \(writePath)") | |
return (newDir, writePath) | |
} catch { | |
print(error) | |
return (nil, nil) | |
} | |
} catch { | |
print(error) | |
return (nil, nil) | |
} | |
} | |
static func imageUrls(for folderPath: URL) -> [URL]? { | |
do { | |
let contents = try FileManager.default.contentsOfDirectory(at: folderPath, includingPropertiesForKeys: nil, options: FileManager.DirectoryEnumerationOptions.skipsHiddenFiles) | |
return contents | |
} catch { | |
print(error) | |
} | |
return nil | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment