increase image cache to 128 MiB and fix perf for calculating image cost
This commit is contained in:
@@ -1250,7 +1250,8 @@ class ImageLoader {
|
|||||||
private var loadingTasks: [String: [((UIImage?) -> Void)]] = [:]
|
private var loadingTasks: [String: [((UIImage?) -> Void)]] = [:]
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
cache.totalCostLimit = 50 * 1024 * 1024
|
// 128 MiB
|
||||||
|
cache.totalCostLimit = 128 * 1024 * 1024
|
||||||
}
|
}
|
||||||
|
|
||||||
func preloadImage(at path: URL) {
|
func preloadImage(at path: URL) {
|
||||||
@@ -1277,13 +1278,13 @@ class ImageLoader {
|
|||||||
|
|
||||||
let image = UIImage(contentsOfFile: path.path)
|
let image = UIImage(contentsOfFile: path.path)
|
||||||
|
|
||||||
DispatchQueue.main.async {
|
if let image = image, let cost = imageByteSize(image) {
|
||||||
if let image = image, let data = image.pngData() {
|
self.cache.setObject(image, forKey: path.path as NSString, cost: cost)
|
||||||
self.cache.setObject(image, forKey: path.path as NSString, cost: data.count)
|
} else {
|
||||||
} else {
|
print("no image to cache found")
|
||||||
print("no image to cache found")
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
DispatchQueue.main.async {
|
||||||
self.loadingTasks[path.path]?.forEach { $0(image) }
|
self.loadingTasks[path.path]?.forEach { $0(image) }
|
||||||
self.loadingTasks.removeValue(forKey: path.path)
|
self.loadingTasks.removeValue(forKey: path.path)
|
||||||
}
|
}
|
||||||
@@ -1294,3 +1295,8 @@ class ImageLoader {
|
|||||||
return cache.object(forKey: path as NSString)
|
return cache.object(forKey: path as NSString)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func imageByteSize(_ image: UIImage) -> Int? {
|
||||||
|
guard let cgImage = image.cgImage else { return nil }
|
||||||
|
return cgImage.bytesPerRow * cgImage.height
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user