@onekeyfe/react-native-image 6.7.0-41 → 6.7.0-42
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/ios/OneKeyImage.swift
CHANGED
|
@@ -183,7 +183,14 @@ final class HybridOneKeyImage: HybridOneKeyImageSpec, RecyclableView {
|
|
|
183
183
|
guard let self else { return }
|
|
184
184
|
self.skeletonIndicator.updateFrame(self.hostView.bounds)
|
|
185
185
|
self.layoutFallbackLayer()
|
|
186
|
-
self.
|
|
186
|
+
if self.attached && !self.hostView.bounds.isEmpty {
|
|
187
|
+
// Layout completes the prop batch, so start asynchronous work now.
|
|
188
|
+
self.loadWorkItem?.cancel()
|
|
189
|
+
self.loadWorkItem = nil
|
|
190
|
+
self.startLoad(force: false)
|
|
191
|
+
} else {
|
|
192
|
+
self.scheduleLoad()
|
|
193
|
+
}
|
|
187
194
|
}
|
|
188
195
|
hostView.onWindowChanged = { [weak self] attached in
|
|
189
196
|
guard let self else { return }
|
package/ios/OneKeyNamedSVG.swift
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import Foundation
|
|
2
|
+
import SDWebImage
|
|
2
3
|
import UIKit
|
|
3
4
|
|
|
4
5
|
private final class OneKeyIconBundleToken: NSObject {}
|
|
@@ -91,6 +92,17 @@ public final class OneKeyNamedSVG {
|
|
|
91
92
|
let finalColor: Bool
|
|
92
93
|
let complete: (RenderResult?) -> Void
|
|
93
94
|
}
|
|
95
|
+
private static let rasterDiskCache: SDImageCache = {
|
|
96
|
+
let config = SDImageCacheConfig()
|
|
97
|
+
config.shouldCacheImagesInMemory = false
|
|
98
|
+
config.shouldUseWeakMemoryCache = false
|
|
99
|
+
// Source digests invalidate changed assets; retain only size-based eviction.
|
|
100
|
+
config.maxDiskAge = -1
|
|
101
|
+
config.maxDiskSize = 128 * 1024 * 1024
|
|
102
|
+
return SDImageCache(namespace: "onekey-named-svg-raster-v1", diskCacheDirectory: nil, config: config)
|
|
103
|
+
}()
|
|
104
|
+
private static let diskWriter = DispatchQueue(label: "onekey.svg.raster-disk", qos: .utility)
|
|
105
|
+
|
|
94
106
|
private let lock = NSLock()
|
|
95
107
|
private let metadataLock = NSLock()
|
|
96
108
|
private var pending: [ObjectIdentifier: Request] = [:]
|
|
@@ -245,6 +257,15 @@ public final class OneKeyNamedSVG {
|
|
|
245
257
|
lock.unlock(); return image
|
|
246
258
|
}
|
|
247
259
|
lock.unlock()
|
|
260
|
+
// Only workers access disk. The source digest and all raster inputs are in the key.
|
|
261
|
+
let diskCache = Self.rasterDiskCache
|
|
262
|
+
let diskImage = diskCache.imageFromDiskCache(forKey: key)
|
|
263
|
+
if let image = diskImage,
|
|
264
|
+
image.cgImage?.width == request.width, image.cgImage?.height == request.height {
|
|
265
|
+
let result = RenderResult(image: image, mode: mode, sourceIdentity: sourceIdentity, maskAlpha: maskAlpha)
|
|
266
|
+
remember(result, key: key)
|
|
267
|
+
return result
|
|
268
|
+
}
|
|
248
269
|
guard let source = try? String(contentsOf: url, encoding: .utf8),
|
|
249
270
|
let pattern = try? NSRegularExpression(pattern: #"((?:fill|stroke)\s*=\s*["'])currentColor(["'])"#) else { return nil }
|
|
250
271
|
// CoreSVG interprets integer rgba alpha as an 8-bit channel; decimals retain CSS endpoints.
|
|
@@ -265,10 +286,22 @@ public final class OneKeyNamedSVG {
|
|
|
265
286
|
width: fitted.width, height: fitted.height))
|
|
266
287
|
}
|
|
267
288
|
let result = RenderResult(image: output, mode: mode, sourceIdentity: sourceIdentity, maskAlpha: maskAlpha)
|
|
268
|
-
|
|
289
|
+
remember(result, key: key)
|
|
290
|
+
if (output.cgImage?.bytesPerRow ?? request.width * 4) * request.height <= 8_388_608 {
|
|
291
|
+
Self.diskWriter.async {
|
|
292
|
+
if let data = output.pngData() {
|
|
293
|
+
Self.rasterDiskCache.storeImageData(toDisk: data, forKey: key)
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
return result
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
private func remember(_ result: RenderResult, key: String) {
|
|
301
|
+
let cost = (result.image.cgImage?.bytesPerRow ?? Int(result.image.size.width) * 4) * (result.image.cgImage?.height ?? Int(result.image.size.height))
|
|
269
302
|
if cost <= 8_388_608 {
|
|
270
303
|
lock.lock(); defer { lock.unlock() }
|
|
271
|
-
if let previous = cache[key] { bytes -= (previous.image.cgImage?.bytesPerRow ??
|
|
304
|
+
if let previous = cache[key] { bytes -= (previous.image.cgImage?.bytesPerRow ?? 0) * (previous.image.cgImage?.height ?? 0) }
|
|
272
305
|
cache[key] = result
|
|
273
306
|
recency.removeAll { $0 == key }; recency.append(key)
|
|
274
307
|
bytes += cost
|
|
@@ -277,6 +310,5 @@ public final class OneKeyNamedSVG {
|
|
|
277
310
|
if let image = cache.removeValue(forKey: oldest) { bytes -= (image.image.cgImage?.bytesPerRow ?? 0) * (image.image.cgImage?.height ?? 0) }
|
|
278
311
|
}
|
|
279
312
|
}
|
|
280
|
-
return result
|
|
281
313
|
}
|
|
282
314
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import UIKit
|
|
2
|
+
import SDWebImage
|
|
2
3
|
import XCTest
|
|
3
4
|
|
|
4
5
|
@testable import OneKeyImage
|
|
@@ -63,6 +64,39 @@ final class OneKeyIconTests: XCTestCase {
|
|
|
63
64
|
return bytes
|
|
64
65
|
}
|
|
65
66
|
|
|
67
|
+
func testDiskRasterSurvivesDecodedMemoryEviction() throws {
|
|
68
|
+
let name = "DotVerSolid"
|
|
69
|
+
let color = "rgba(0,0,0,1)"
|
|
70
|
+
let scale: CGFloat = 1.375
|
|
71
|
+
let ownerBundle = Bundle(for: HybridOneKeyIcon.self)
|
|
72
|
+
let bundleURL = try XCTUnwrap(ownerBundle.url(forResource: "OneKeyIcons", withExtension: "bundle")
|
|
73
|
+
?? Bundle.main.url(forResource: "OneKeyIcons", withExtension: "bundle"))
|
|
74
|
+
let bundle = try XCTUnwrap(Bundle(url: bundleURL))
|
|
75
|
+
let manifestURL = try XCTUnwrap(bundle.url(forResource: "manifest", withExtension: "json"))
|
|
76
|
+
let manifest = try XCTUnwrap(JSONSerialization.jsonObject(with: Data(contentsOf: manifestURL)) as? [String: [String: Any]])
|
|
77
|
+
let entry = try XCTUnwrap(manifest[name])
|
|
78
|
+
let digest = try XCTUnwrap(entry["digest"] as? String)
|
|
79
|
+
let mode = entry["recolorMode"] as? String ?? "raster"
|
|
80
|
+
let colorKey = mode == "raster" ? color : mode == "multiple" ? "1.0" : ""
|
|
81
|
+
let key = "\(name)|\(digest)|37|29|\(scale)|\(mode)|\(colorKey)"
|
|
82
|
+
let disk = SDImageCache(namespace: "onekey-named-svg-raster-v1")
|
|
83
|
+
let original = try XCTUnwrap(resolve(name, color: color, width: 37, height: 29, scale: scale))
|
|
84
|
+
eventually("Worker output is persisted as a PNG") { disk.diskImageData(forKey: key) != nil }
|
|
85
|
+
let persisted = try XCTUnwrap(disk.imageFromDiskCache(forKey: key))
|
|
86
|
+
XCTAssertEqual(persisted.cgImage?.width, 37)
|
|
87
|
+
XCTAssertEqual(persisted.cgImage?.height, 29)
|
|
88
|
+
XCTAssertEqual(try pixels(persisted), try pixels(original))
|
|
89
|
+
|
|
90
|
+
// Exhaust the real decoded LRU; the next request must leave its memory fast path.
|
|
91
|
+
for width in 122...250 {
|
|
92
|
+
XCTAssertNotNil(resolve(name, color: color, width: width, height: 25, scale: scale))
|
|
93
|
+
}
|
|
94
|
+
XCTAssertNil(OneKeyNamedSVG.shared.cachedRender(name: name, color: color,
|
|
95
|
+
width: 37, height: 29, scale: scale))
|
|
96
|
+
let restored = try XCTUnwrap(resolve(name, color: color, width: 37, height: 29, scale: scale))
|
|
97
|
+
XCTAssertEqual(try pixels(restored), try pixels(original))
|
|
98
|
+
}
|
|
99
|
+
|
|
66
100
|
func testNamedColorEndpointsRetainCSSAlpha() throws {
|
|
67
101
|
for (alpha, expected) in [("1", 255), ("0.5", 128), ("0", 0)] {
|
|
68
102
|
let image = try XCTUnwrap(resolve("DotVerSolid", color: "rgba(255,0,0,\(alpha))", width: 192, height: 192))
|
package/package.json
CHANGED