Skip to content

Commit 0762f51

Browse files
authored
Merge pull request #65 from SDWebImage/refactory_new_animated_image_watchOS
Drop WatchKit hack. Supports Native SwiftUI animated image, using SDAnimatedImagePlayer
2 parents 0a176f4 + 411255b commit 0762f51

15 files changed

+117
-760
lines changed

Example/SDWebImageSwiftUIDemo/ContentView.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ struct ContentView: View {
3838
"https://raw.githubusercontent.com/icons8/flat-color-icons/master/pdf/stack_of_photos.pdf",
3939
"https://raw.githubusercontent.com/icons8/flat-color-icons/master/pdf/smartphone_tablet.pdf"
4040
]
41-
@State var animated: Bool = false // You can change between WebImage/AnimatedImage
41+
@State var animated: Bool = true // You can change between WebImage/AnimatedImage
4242

4343
var body: some View {
4444
#if os(iOS) || os(tvOS)
@@ -105,8 +105,16 @@ struct ContentView: View {
105105
.scaledToFit()
106106
.frame(width: CGFloat(100), height: CGFloat(100), alignment: .center)
107107
#else
108-
AnimatedImage(url: URL(string:url))
108+
WebImage(url: URL(string:url))
109109
.resizable()
110+
.animated()
111+
.indicator { _, _ in
112+
ActivityBar()
113+
.foregroundColor(Color.white)
114+
.frame(width: 50, height: 50)
115+
}
116+
.animation(.easeInOut(duration: 0.5))
117+
.transition(.fade)
110118
.scaledToFit()
111119
.frame(width: CGFloat(100), height: CGFloat(100), alignment: .center)
112120
#endif

Example/SDWebImageSwiftUIDemo/DetailView.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,14 @@ struct DetailView: View {
9595
.resizable()
9696
.scaledToFit()
9797
#else
98-
AnimatedImage(url: URL(string:url), options: [.progressiveLoad], isAnimating: $isAnimating)
98+
WebImage(url: URL(string:url), options: [.progressiveLoad])
9999
.resizable()
100+
.animated(isAnimating)
101+
.indicator { isAnimating, progress in
102+
ProgressBar(value: progress)
103+
.foregroundColor(.blue)
104+
.frame(maxHeight: 6)
105+
}
100106
.scaledToFit()
101107
#endif
102108
} else {

Package.swift

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,9 @@ let package = Package(
2424
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
2525
.target(
2626
name: "SDWebImageSwiftUI",
27-
dependencies: ["SDWebImage", "SDWebImageSwiftUIObjC"],
27+
dependencies: ["SDWebImage"],
2828
path: "SDWebImageSwiftUI/Classes",
2929
exclude: ["ObjC"]
3030
),
31-
// This is implementation detail because SwiftPM does not support mixed Objective-C/Swift code, don't dependent this target
32-
.target(
33-
name: "SDWebImageSwiftUIObjC",
34-
dependencies: ["SDWebImage"],
35-
path: "SDWebImageSwiftUI/Classes/ObjC",
36-
publicHeadersPath: "."
37-
)
3831
]
3932
)

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ let package = Package(
8686
- [x] Supports success/failure/progress changes event for custom handling
8787
- [x] Supports indicator with activity/progress indicator and customization
8888
- [x] Supports built-in animation and transition, powered by SwiftUI
89+
- [x] Supports animated image as well ! (from v0.9.0)
8990

9091
```swift
9192
var body: some View {
@@ -99,6 +100,7 @@ var body: some View {
99100
.placeholder {
100101
Rectangle().foregroundColor(.gray)
101102
}
103+
.animated() // Supports Animated Image
102104
.indicator(.activity) // Activity Indicator
103105
.animation(.easeInOut(duration: 0.5)) // Animation Duration
104106
.transition(.fade) // Fade Transition
@@ -109,6 +111,8 @@ var body: some View {
109111

110112
Note: This `WebImage` using `Image` for internal implementation, which is the best compatible for SwiftUI layout and animation system. But it supports static image format only, because unlike `UIImageView` in UIKit, SwiftUI's `Image` does not support animated image or vector image.
111113

114+
Note: From v0.9.0, `WebImage` supports animated image as well ! You can use `.animated()` to start animation. This is done by using the native SwiftUI rendering system and SDWebImage's powerful [Animated Player](https://github.com/SDWebImage/SDWebImage/wiki/Advanced-Usage#animated-player-530). But it's still recommend to use `AnimatedImage` for advanced controls like progressive animation rendering, runloop mode, playback rate, etc.
115+
112116
### Using `AnimatedImage` to play animation
113117

114118
- [x] Supports network image as well as local data and bundle image
@@ -150,17 +154,19 @@ var body: some View {
150154

151155
Note: `AnimatedImage` supports both image url or image data for animated image format. Which use the SDWebImage's [Animated ImageView](https://github.com/SDWebImage/SDWebImage/wiki/Advanced-Usage#animated-image-50) for internal implementation. Pay attention that since this base on UIKit/AppKit representable, some advanced SwiftUI layout and animation system may not work as expected. You may need UIKit/AppKit and Core Animation to modify the native view.
152156

153-
Note: From v0.8.0, `AnimatedImage` on watchOS support all features the same as iOS/tvOS/macOS, including Animated WebP rendering, runloop mode, pausable, purgeable, playback rate, etc. It use the SDWebImage's [Animated Player](https://github.com/SDWebImage/SDWebImage/wiki/Advanced-Usage#animated-player-530), which is the same backend engine for iOS/tvOS/macOS's Animated ImageView.
157+
Note: From v0.9.0, `AnimatedImage` on watchOS drop the supports on watchOS, because of using hacks and private APIs. For watchOS user, choose `WebImage` instead.
154158

155-
Note: From v0.4.0, `AnimatedImage` supports watchOS as well. However, it's not backed by SDWebImage's [Animated ImageView](https://github.com/SDWebImage/SDWebImage/wiki/Advanced-Usage#animated-image-50) like iOS/tvOS/macOS. It use some tricks and hacks because of the limitation on current Apple's API. It also use Image/IO decoding system, which means it supports GIF and APNG format only, but not external format like Animated WebP.
159+
Note: From v0.8.0, `AnimatedImage` on watchOS support all features the same as iOS/tvOS/macOS, including Animated WebP rendering, runloop mode, pausable, purgeable, playback rate, etc. It use the SDWebImage's [Animated Player](https://github.com/SDWebImage/SDWebImage/wiki/Advanced-Usage#animated-player-530), which is the same backend engine for iOS/tvOS/macOS's Animated ImageView.
156160

157161
### Which View to choose
158162

159163
Why we have two different View types here, is because of current SwiftUI limit. But we're aimed to provide best solution for all use cases.
160164

161165
If you don't need animated image, prefer to use `WebImage` firstly. Which behaves the seamless as built-in SwiftUI View. If SwiftUI works, it works.
162166

163-
If you need animated image, `AnimatedImage` is the one to choose. Remember it supports static image as well, you don't need to check the format, just use as it.
167+
If you need simple animated image, use v0.9.0 above with `WebImage`. Which provide the animated image support.
168+
169+
If you need powerful animated image, `AnimatedImage` is the one to choose. Remember it supports static image as well, you don't need to check the format, just use as it.
164170

165171
But, because `AnimatedImage` use `UIViewRepresentable` and driven by UIKit, currently there may be some small incompatible issues between UIKit and SwiftUI layout and animation system, or bugs related to SwiftUI itself. We try our best to match SwiftUI behavior, and provide the same API as `WebImage`, which make it easy to switch between these two types if needed.
166172

0 commit comments

Comments
 (0)