Last active
April 14, 2020 06:54
-
-
Save navsing/90e4fa186e25952169c37269e698413d 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
class RemoteImageModel: ObservableObject { | |
@Published var displayImage: UIImage? | |
var imageUrl: String? | |
var cachedImage = CachedImage.getCachedImage() | |
init(imageUrl: String?) { | |
self.imageUrl = imageUrl | |
if imageFromCache() { | |
return | |
} | |
imageFromRemoteUrl() | |
} | |
func imageFromCache() -> Bool { | |
guard let url = imageUrl, let cacheImage = cachedImage.get(key: url) else { | |
return false | |
} | |
displayImage = cacheImage | |
return true | |
} | |
func imageFromRemoteUrl() { | |
guard let url = imageUrl else { | |
return | |
} | |
let imageURL = URL(string: url)! | |
URLSession.shared.dataTask(with: imageURL, completionHandler: { (data, response, error) in | |
if let data = data { | |
DispatchQueue.main.async { | |
guard let remoteImage = UIImage(data: data) else { | |
return | |
} | |
self.cachedImage.set(key: self.imageUrl!, image: remoteImage) | |
self.displayImage = remoteImage | |
} | |
} | |
}).resume() | |
} | |
} | |
class CachedImage { | |
var cache = NSCache<NSString, UIImage>() | |
func get(key: String) -> UIImage? { | |
return cache.object(forKey: NSString(string: key)) | |
} | |
func set(key: String, image: UIImage) { | |
cache.setObject(image, forKey: NSString(string: key)) | |
} | |
} | |
extension CachedImage { | |
private static var cachedImage = CachedImage() | |
static func getCachedImage() -> CachedImage { | |
return cachedImage | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment