Skip to content

Commit d17d44e

Browse files
committed
Remove the WebImage placeholder maxWidth/maxHeight modifier, this may break some use case like TabView. If user want to use placeholder, limit themselves
Also, polish the player API
1 parent e19c35a commit d17d44e

File tree

2 files changed

+47
-24
lines changed

2 files changed

+47
-24
lines changed

SDWebImageSwiftUI/Classes/ImagePlayer.swift

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ public final class ImagePlayer : ObservableObject {
3737
/// Current playing frame image
3838
@Published public var currentFrame: PlatformImage?
3939

40+
/// Current playing frame index
41+
@Published public var currentFrameIndex: UInt = 0
42+
43+
/// Current playing loop count
44+
@Published public var currentLoopCount: UInt = 0
45+
46+
/// Whether current player is valid for playing. This will check the internal player exist or not
47+
public var isValid: Bool {
48+
player != nil
49+
}
50+
51+
/// Current playing status
52+
public var isPlaying: Bool {
53+
player?.isPlaying ?? false
54+
}
55+
4056
/// Start the animation
4157
public func startPlaying() {
4258
player?.startPlaying()
@@ -52,38 +68,44 @@ public final class ImagePlayer : ObservableObject {
5268
player?.stopPlaying()
5369
}
5470

71+
/// Seek to frame and loop count
72+
public func seekToFrame(at: UInt, loopCount: UInt) {
73+
player?.seekToFrame(at: at, loopCount: loopCount)
74+
}
75+
5576
/// Clear the frame buffer
5677
public func clearFrameBuffer() {
5778
player?.clearFrameBuffer()
5879
}
5980

60-
6181
/// Setup the player using Animated Image
6282
/// - Parameter image: animated image
63-
public func setupPlayer(image: PlatformImage?) {
64-
if player != nil {
83+
public func setupPlayer(animatedImage: SDAnimatedImageProvider) {
84+
if isValid {
6585
return
6686
}
67-
if let animatedImage = image as? SDAnimatedImageProvider & PlatformImage {
68-
if let imagePlayer = SDAnimatedImagePlayer(provider: animatedImage) {
69-
imagePlayer.animationFrameHandler = { [weak self] (_, frame) in
70-
self?.currentFrame = frame
71-
}
72-
// Setup configuration
73-
if let maxBufferSize = maxBufferSize {
74-
imagePlayer.maxBufferSize = maxBufferSize
75-
}
76-
if let customLoopCount = customLoopCount {
77-
imagePlayer.totalLoopCount = customLoopCount
78-
}
79-
imagePlayer.runLoopMode = runLoopMode
80-
imagePlayer.playbackRate = playbackRate
81-
imagePlayer.playbackMode = playbackMode
82-
83-
self.player = imagePlayer
84-
85-
imagePlayer.startPlaying()
87+
if let imagePlayer = SDAnimatedImagePlayer(provider: animatedImage) {
88+
imagePlayer.animationFrameHandler = { [weak self] (index, frame) in
89+
self?.currentFrameIndex = index
90+
self?.currentFrame = frame
91+
}
92+
imagePlayer.animationLoopHandler = { [weak self] (loopCount) in
93+
self?.currentLoopCount = loopCount
94+
}
95+
// Setup configuration
96+
if let maxBufferSize = maxBufferSize {
97+
imagePlayer.maxBufferSize = maxBufferSize
98+
}
99+
if let customLoopCount = customLoopCount {
100+
imagePlayer.totalLoopCount = customLoopCount
86101
}
102+
imagePlayer.runLoopMode = runLoopMode
103+
imagePlayer.playbackRate = playbackRate
104+
imagePlayer.playbackMode = playbackMode
105+
106+
self.player = imagePlayer
107+
108+
imagePlayer.startPlaying()
87109
}
88110
}
89111
}

SDWebImageSwiftUI/Classes/WebImage.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ public struct WebImage : View {
8080
} else {
8181
configure(image: imageManager.image!)
8282
.onReceive(imageManager.$image) { image in
83-
self.imagePlayer.setupPlayer(image: image)
83+
if let animatedImage = image as? SDAnimatedImageProvider {
84+
self.imagePlayer.setupPlayer(animatedImage: animatedImage)
85+
}
8486
}
8587
}
8688
} else {
@@ -92,7 +94,6 @@ public struct WebImage : View {
9294
}
9395
} else {
9496
setupPlaceholder()
95-
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
9697
.onPlatformAppear(appear: {
9798
// Load remote image when first appear
9899
if self.imageManager.isFirstLoad {

0 commit comments

Comments
 (0)