mirror of
https://github.com/ivanvorobei/SwiftUI.git
synced 2026-01-15 21:53:29 +01:00
Add Async image loading
This commit is contained in:
27
Examples/Async image loading/RemoteImage/ImageCache.swift
Executable file
27
Examples/Async image loading/RemoteImage/ImageCache.swift
Executable file
@@ -0,0 +1,27 @@
|
||||
//
|
||||
// File 2.swift
|
||||
//
|
||||
//
|
||||
// Created by Callum Trounce on 05/06/2019.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
|
||||
@available(iOS 13.0, *)
|
||||
class ImageCache {
|
||||
|
||||
private let cache = NSCache<NSURL, CGImage>()
|
||||
|
||||
private let queue = DispatchQueue.init(label: "cacheQueue", qos: .userInteractive)
|
||||
|
||||
func store(image: CGImage, for url: URL) {
|
||||
queue.async { [unowned cache] in
|
||||
cache.setObject(image, forKey: url as NSURL)
|
||||
}
|
||||
}
|
||||
|
||||
func image(for url: URL) -> CGImage? {
|
||||
return cache.object(forKey: url as NSURL)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user