From d07a405bb5ff342917b5175f1dd86a28000ae218 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Sat, 4 Apr 2020 19:06:23 +0800 Subject: [PATCH 1/2] Upgrade the dependency of SDWebImage 5.7.0 --- Cartfile | 2 +- Package.resolved | 4 +- Package.swift | 2 +- SDWebImageSwiftUI.podspec | 2 +- SDWebImageSwiftUI/Classes/ImageManager.swift | 46 ++++---------------- 5 files changed, 13 insertions(+), 43 deletions(-) diff --git a/Cartfile b/Cartfile index 6fc38db..4d1cc4d 100644 --- a/Cartfile +++ b/Cartfile @@ -1 +1 @@ -github "SDWebImage/SDWebImage" ~> 5.3 \ No newline at end of file +github "SDWebImage/SDWebImage" ~> 5.7 \ No newline at end of file diff --git a/Package.resolved b/Package.resolved index 5410a19..4c1bce3 100644 --- a/Package.resolved +++ b/Package.resolved @@ -6,8 +6,8 @@ "repositoryURL": "https://github.com/SDWebImage/SDWebImage.git", "state": { "branch": null, - "revision": "48909c2a744d9c7d0350398fafc127945e367286", - "version": "5.3.0" + "revision": "e2285181a62daf4d1d3caf66d6d776b667092303", + "version": "5.7.0" } } ] diff --git a/Package.swift b/Package.swift index 91c33a1..f33d13f 100644 --- a/Package.swift +++ b/Package.swift @@ -17,7 +17,7 @@ let package = Package( dependencies: [ // Dependencies declare other packages that this package depends on. // .package(url: /* package url */, from: "1.0.0"), - .package(url: "https://github.com/SDWebImage/SDWebImage.git", from: "5.3.0") + .package(url: "https://github.com/SDWebImage/SDWebImage.git", from: "5.7.0") ], targets: [ // Targets are the basic building blocks of a package. A target can define a module or a test suite. diff --git a/SDWebImageSwiftUI.podspec b/SDWebImageSwiftUI.podspec index 73191fe..5746159 100644 --- a/SDWebImageSwiftUI.podspec +++ b/SDWebImageSwiftUI.podspec @@ -34,6 +34,6 @@ It brings all your favorite features from SDWebImage, like async image loading, } s.weak_frameworks = 'SwiftUI', 'Combine' - s.dependency 'SDWebImage', '~> 5.3' + s.dependency 'SDWebImage', '~> 5.7' s.swift_version = '5.1' end diff --git a/SDWebImageSwiftUI/Classes/ImageManager.swift b/SDWebImageSwiftUI/Classes/ImageManager.swift index 279fa55..7fca6ba 100644 --- a/SDWebImageSwiftUI/Classes/ImageManager.swift +++ b/SDWebImageSwiftUI/Classes/ImageManager.swift @@ -110,45 +110,15 @@ public final class ImageManager : ObservableObject { /// Prefetch the initial state of image, currently query the memory cache only func prefetch() { isFirstPrefetch = false - // Use the options processor if provided - let options = self.options - var context = self.context - if let result = manager.optionsProcessor?.processedResult(for: url, options: options, context: context) { - context = result.context - } - // TODO: Remove transformer for cache calculation before SDWebImage 5.7.0, this is bug. Remove later - let transformer = (context?[.imageTransformer] as? SDImageTransformer) ?? manager.transformer - context?[.imageTransformer] = nil - // TODO: before SDWebImage 5.7.0, this is the SPI. Remove later - var key: String? - let selector = Selector(("cacheKeyForURL:context:")) - if manager.responds(to: selector) { - key = manager.perform(selector, with: url, with: context)?.takeUnretainedValue() as? String - } else { - key = manager.cacheKey(for: url) - } - if let transformer = transformer { - key = SDTransformedKeyForKey(key, transformer.transformerKey) - } - // Shortcut for built-in cache - if let imageCache = manager.imageCache as? SDImageCache { - let image = imageCache.imageFromMemoryCache(forKey: key) + var options = self.options + // use `.fromCacheOnly` to query cache only + options.insert(.fromCacheOnly) + var context = self.context ?? [:] + context[.queryCacheType] = SDImageCacheType.memory.rawValue + // Use `.queryCacheType` to query memory cache only + manager.loadImage(with: url, options: options, context: context, progress: nil) { (image, data, error, cacheType, finished, imageUrl) in + // This will callback immediately self.image = image - if let image = image { - self.successBlock?(image, .memory) - } - } else { - // This callback is synchronzied - manager.imageCache.containsImage(forKey: key, cacheType: .memory) { [unowned self] (cacheType) in - if cacheType == .memory { - self.manager.imageCache.queryImage(forKey: key, options: options, context: context) { [unowned self] (image, data, cacheType) in - self.image = image - if let image = image { - self.successBlock?(image, cacheType) - } - } - } - } } } From 4613ebfbdebe7af54752980a44b9b9355bcbbc14 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Sun, 5 Apr 2020 12:15:22 +0800 Subject: [PATCH 2/2] Don't do prefetch from memory cache if user indeed pass `.fromLoaderOnly` --- SDWebImageSwiftUI/Classes/ImageManager.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/SDWebImageSwiftUI/Classes/ImageManager.swift b/SDWebImageSwiftUI/Classes/ImageManager.swift index 7fca6ba..f72b674 100644 --- a/SDWebImageSwiftUI/Classes/ImageManager.swift +++ b/SDWebImageSwiftUI/Classes/ImageManager.swift @@ -111,7 +111,11 @@ public final class ImageManager : ObservableObject { func prefetch() { isFirstPrefetch = false var options = self.options - // use `.fromCacheOnly` to query cache only + if options.contains(.fromLoaderOnly) { + // If user indeed ignore cache, don't do prefetch + return + } + // Use `.fromCacheOnly` to query cache only options.insert(.fromCacheOnly) var context = self.context ?? [:] context[.queryCacheType] = SDImageCacheType.memory.rawValue