Skip to content

Commit ee8ccc3

Browse files
authored
Merge pull request #24 from SDWebImage/performance_avoid_multiple_query
Performance enhancement to WebImage/AnimatedImage, to avoid extra query for SDWebImage
2 parents 4cf3d42 + cbf4278 commit ee8ccc3

File tree

2 files changed

+30
-23
lines changed

2 files changed

+30
-23
lines changed

SDWebImageSwiftUI/Classes/AnimatedImage.swift

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,24 @@ public struct AnimatedImage : PlatformViewRepresentable {
172172
}
173173
#endif
174174

175+
func loadImage(_ view: AnimatedImageViewWrapper, url: URL) {
176+
let operationKey = NSStringFromClass(type(of: view.wrapped))
177+
let currentOperation = view.wrapped.sd_imageLoadOperation(forKey: operationKey)
178+
if currentOperation != nil {
179+
return
180+
}
181+
view.wrapped.sd_setImage(with: url, placeholderImage: placeholder, options: webOptions, context: webContext, progress: { (receivedSize, expectedSize, _) in
182+
self.imageModel.progressBlock?(receivedSize, expectedSize)
183+
}) { (image, error, cacheType, _) in
184+
if let image = image {
185+
self.imageModel.image = image
186+
self.imageModel.successBlock?(image, cacheType)
187+
} else {
188+
self.imageModel.failureBlock?(error ?? NSError())
189+
}
190+
}
191+
}
192+
175193
func makeView(context: PlatformViewRepresentableContext<AnimatedImage>) -> AnimatedImageViewWrapper {
176194
AnimatedImageViewWrapper()
177195
}
@@ -185,16 +203,7 @@ public struct AnimatedImage : PlatformViewRepresentable {
185203
#endif
186204
} else {
187205
if let url = url {
188-
view.wrapped.sd_setImage(with: url, placeholderImage: placeholder, options: webOptions, context: webContext, progress: { (receivedSize, expectedSize, _) in
189-
self.imageModel.progressBlock?(receivedSize, expectedSize)
190-
}) { (image, error, cacheType, _) in
191-
self.imageModel.image = image
192-
if let image = image {
193-
self.imageModel.successBlock?(image, cacheType)
194-
} else {
195-
self.imageModel.failureBlock?(error ?? NSError())
196-
}
197-
}
206+
loadImage(view, url: url)
198207
}
199208
}
200209

@@ -351,15 +360,6 @@ public struct AnimatedImage : PlatformViewRepresentable {
351360

352361
// Antialiased
353362
view.shouldAntialias = imageLayout.antialiased
354-
355-
// Display
356-
#if os(macOS)
357-
view.needsLayout = true
358-
view.needsDisplay = true
359-
#else
360-
view.setNeedsLayout()
361-
view.setNeedsDisplay()
362-
#endif
363363
#endif
364364
}
365365

SDWebImageSwiftUI/Classes/ImageManager.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,24 @@ class ImageManager : ObservableObject {
2929
}
3030

3131
func load() {
32+
if currentOperation != nil {
33+
return
34+
}
3235
currentOperation = manager.loadImage(with: url, options: options, context: context, progress: { [weak self] (receivedSize, expectedSize, _) in
3336
self?.progressBlock?(receivedSize, expectedSize)
34-
}) { [weak self] (image, data, error, cacheType, _, _) in
37+
}) { [weak self] (image, data, error, cacheType, finished, _) in
3538
guard let self = self else {
3639
return
3740
}
3841
if let image = image {
3942
self.image = image
40-
self.successBlock?(image, cacheType)
41-
} else {
42-
self.failureBlock?(error ?? NSError())
43+
}
44+
if finished {
45+
if let image = image {
46+
self.successBlock?(image, cacheType)
47+
} else {
48+
self.failureBlock?(error ?? NSError())
49+
}
4350
}
4451
}
4552
}

0 commit comments

Comments
 (0)