Skip to content

Commit d86904f

Browse files
committed
Add support to custom loop count / max buffer size / incremental load, which is already supported by SDAnimatedImageView
1 parent 8dc9dfc commit d86904f

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

SDWebImageSwiftUI/Classes/AnimatedImage.swift

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,18 @@ final class AnimatedImageLayout : ObservableObject {
3131
@Published var antialiased: Bool = false
3232
}
3333

34+
// Configuration Binding Object
35+
final class AnimatedImageConfiguration: ObservableObject {
36+
@Published var incrementalLoad: Bool?
37+
@Published var maxBufferSize: UInt?
38+
@Published var customLoopCount: Int?
39+
}
40+
3441
// View
3542
public struct AnimatedImage : PlatformViewRepresentable {
3643
@ObservedObject var imageModel = AnimatedImageModel()
3744
@ObservedObject var imageLayout = AnimatedImageLayout()
45+
@ObservedObject var imageConfiguration = AnimatedImageConfiguration()
3846

3947
var placeholder: PlatformImage?
4048
var webOptions: SDWebImageOptions = []
@@ -82,6 +90,7 @@ public struct AnimatedImage : PlatformViewRepresentable {
8290
}
8391
}
8492

93+
configureView(view, context: context)
8594
layoutView(view, context: context)
8695
}
8796

@@ -190,6 +199,30 @@ public struct AnimatedImage : PlatformViewRepresentable {
190199
view.setNeedsDisplay()
191200
#endif
192201
}
202+
203+
func configureView(_ view: AnimatedImageViewWrapper, context: PlatformViewRepresentableContext<AnimatedImage>) {
204+
// IncrementalLoad
205+
if let incrementalLoad = imageConfiguration.incrementalLoad {
206+
view.wrapped.shouldIncrementalLoad = incrementalLoad
207+
}
208+
209+
// MaxBufferSize
210+
if let maxBufferSize = imageConfiguration.maxBufferSize {
211+
view.wrapped.maxBufferSize = maxBufferSize
212+
} else {
213+
// automatically
214+
view.wrapped.maxBufferSize = 0
215+
}
216+
217+
// CustomLoopCount
218+
if let customLoopCount = imageConfiguration.customLoopCount {
219+
view.wrapped.shouldCustomLoopCount = true
220+
view.wrapped.animationRepeatCount = customLoopCount
221+
} else {
222+
// disable custom loop count
223+
view.wrapped.shouldCustomLoopCount = false
224+
}
225+
}
193226
}
194227

195228
// Layout
@@ -241,6 +274,24 @@ extension AnimatedImage {
241274
}
242275
}
243276

277+
// AnimatedImage Modifier
278+
extension AnimatedImage {
279+
public func customLoopCount(_ loopCount: Int?) -> AnimatedImage {
280+
imageConfiguration.customLoopCount = loopCount
281+
return self
282+
}
283+
284+
public func maxBufferSize(_ bufferSize: UInt?) -> AnimatedImage {
285+
imageConfiguration.maxBufferSize = bufferSize
286+
return self
287+
}
288+
289+
public func incrementalLoad(_ incrementalLoad: Bool) -> AnimatedImage {
290+
imageConfiguration.incrementalLoad = incrementalLoad
291+
return self
292+
}
293+
}
294+
244295
// Completion Handler
245296
extension AnimatedImage {
246297
public func onFailure(perform action: ((Error) -> Void)? = nil) -> AnimatedImage {

0 commit comments

Comments
 (0)