Skip to content

Fix the issue when using WebImage with some transition like scaleEffect, each time the new state update will cause unused image fetching #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Example/SDWebImageSwiftUIDemo/DetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct DetailView: View {
let url: String
let animated: Bool
@State var isAnimating: Bool = true
@State var lastScaleValue: CGFloat = 1.0
@State var lastScale: CGFloat = 1.0
@State var scale: CGFloat = 1.0
@Environment(\.presentationMode) var presentationMode
@EnvironmentObject var settings: UserSettings
Expand Down Expand Up @@ -75,12 +75,12 @@ struct DetailView: View {
return contentView()
.scaleEffect(self.scale)
.gesture(MagnificationGesture(minimumScaleDelta: 0.1).onChanged { value in
let delta = value / self.lastScaleValue
self.lastScaleValue = value
let delta = value / self.lastScale
self.lastScale = value
let newScale = self.scale * delta
self.scale = min(max(newScale, 0.5), 2)
}.onEnded { value in
self.lastScaleValue = 1.0
self.lastScale = 1.0
})
#endif
#if os(tvOS)
Expand Down
40 changes: 40 additions & 0 deletions SDWebImageSwiftUI/Classes/ImageManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public final class ImageManager : ObservableObject {
var manager: SDWebImageManager
weak var currentOperation: SDWebImageOperation? = nil
var isFirstLoad: Bool = true // false after first call `load()`
var isFirstPrefetch: Bool = true // false after first call `prefetch()`

var url: URL?
var options: SDWebImageOptions
Expand Down Expand Up @@ -106,6 +107,45 @@ 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 = manager.perform(Selector(("cacheKeyForURL:context:")), with: url, with: context)?.takeUnretainedValue() as? String
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)
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)
}
}
}
}
}
}

}

// Completion Handler
Expand Down
14 changes: 9 additions & 5 deletions SDWebImageSwiftUI/Classes/WebImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,10 @@ public struct WebImage : View {
}

public var body: some View {
// load remote image when first called `body`, SwiftUI sometimes will create a new View struct without calling `onAppear` (like enter EditMode) :)
// this can ensure we load the image, and display image synchronously when memory cache hit to avoid flashing
// called once per struct, SDWebImage take care of the duplicated query
if imageManager.isFirstLoad {
imageManager.load()
// this prefetch the memory cache of image, to immediately render it on screen
// this solve the case when `onAppear` not been called, for example, some transaction indeterminate state, SwiftUI :)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why SwiftUI Suck here ? The layout engine create a new View struct, touch the body value, but not trigger the onAppear, what does this used for ?

if imageManager.isFirstPrefetch {
self.imageManager.prefetch()
}
return Group {
if imageManager.image != nil {
Expand Down Expand Up @@ -109,6 +108,11 @@ public struct WebImage : View {
setupPlaceholder()
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.onAppear {
// load remote image when first appear
if self.imageManager.isFirstLoad {
self.imageManager.load()
return
}
guard self.retryOnAppear else { return }
// When using prorgessive loading, the new partial image will cause onAppear. Filter this case
if self.imageManager.image == nil && !self.imageManager.isIncremental {
Expand Down