@@ -63,13 +63,13 @@ public struct WebImage : View {
63
63
// this prefetch the memory cache of image, to immediately render it on screen
64
64
// this solve the case when `onAppear` not been called, for example, some transaction indeterminate state, SwiftUI :)
65
65
if imageManager. isFirstPrefetch {
66
- self . imageManager. prefetch ( )
66
+ imageManager. prefetch ( )
67
67
}
68
68
return Group {
69
69
if imageManager. image != nil {
70
- if isAnimating && !self . imageManager. isIncremental {
70
+ if isAnimating && !imageManager. isIncremental {
71
71
if currentFrame != nil {
72
- configure ( image: Image ( platformImage : currentFrame!) )
72
+ configure ( image: currentFrame!)
73
73
. onAppear {
74
74
self . imagePlayer? . startPlaying ( )
75
75
}
@@ -84,16 +84,16 @@ public struct WebImage : View {
84
84
}
85
85
}
86
86
} else {
87
- configure ( image: Image ( platformImage : imageManager. image!) )
87
+ configure ( image: imageManager. image!)
88
88
. onReceive ( imageManager. $image) { image in
89
89
self . setupPlayer ( image: image)
90
90
}
91
91
}
92
92
} else {
93
93
if currentFrame != nil {
94
- configure ( image: Image ( platformImage : currentFrame!) )
94
+ configure ( image: currentFrame!)
95
95
} else {
96
- configure ( image: Image ( platformImage : imageManager. image!) )
96
+ configure ( image: imageManager. image!)
97
97
}
98
98
}
99
99
} else {
@@ -122,10 +122,47 @@ public struct WebImage : View {
122
122
}
123
123
}
124
124
125
- func configure( image: Image ) -> some View {
125
+ /// Configure the platform image into the SwiftUI rendering image
126
+ func configure( image: PlatformImage ) -> some View {
127
+ var image = image
128
+ // Actual rendering SwiftUI image
129
+ let result : Image
130
+ // NSImage works well with SwiftUI, include Animated and Vector Image
131
+ #if os(macOS)
132
+ result = Image ( nsImage: image)
133
+ #else
134
+ // Fix the SwiftUI.Image rendering issue when use UIImage based, the `.aspectRatio` does not works. SwiftUI's Bug :)
135
+ // See issue #101
136
+ // Case 1: UIAnimatedImage, grab poster image
137
+ if image. sd_isAnimated {
138
+ // check images property
139
+ if let images = image. images, images. count > 0 {
140
+ image = images [ 0 ]
141
+ }
142
+ }
143
+ // Case 2: Vector Image, draw bitmap image
144
+ else if image. sd_isVector {
145
+ // ensure CGImage is nil
146
+ if image. cgImage == nil {
147
+ // draw vector into bitmap with the screen scale (behavior like AppKit)
148
+ UIGraphicsBeginImageContextWithOptions ( image. size, false , UIScreen . main. scale)
149
+ image. draw ( at: . zero)
150
+ image = UIGraphicsGetImageFromCurrentImageContext ( ) ?? . empty
151
+ UIGraphicsEndImageContext ( )
152
+ }
153
+ }
154
+ // If we have CGImage, use CGImage based API, else use UIImage based API
155
+ if let cgImage = image. cgImage {
156
+ let orientation = image. imageOrientation. toSwiftUI
157
+ result = Image ( decorative: cgImage, scale: image. scale, orientation: orientation)
158
+ } else {
159
+ result = Image ( uiImage: image)
160
+ }
161
+ #endif
162
+
126
163
// Should not use `EmptyView`, which does not respect to the container's frame modifier
127
164
// Using a empty image instead for better compatible
128
- configurations. reduce ( image ) { ( previous, configuration) in
165
+ return configurations. reduce ( result ) { ( previous, configuration) in
129
166
configuration ( previous)
130
167
}
131
168
}
@@ -136,12 +173,12 @@ public struct WebImage : View {
136
173
if let placeholder = placeholder {
137
174
// If use `.delayPlaceholder`, the placeholder is applied after loading failed, hide during loading :)
138
175
if imageManager. options. contains ( . delayPlaceholder) && imageManager. isLoading {
139
- return AnyView ( configure ( image: Image . empty) )
176
+ return AnyView ( configure ( image: . empty) )
140
177
} else {
141
178
return placeholder
142
179
}
143
180
} else {
144
- return AnyView ( configure ( image: Image . empty) )
181
+ return AnyView ( configure ( image: . empty) )
145
182
}
146
183
}
147
184
@@ -260,7 +297,9 @@ extension WebImage {
260
297
/// - Parameter image: A Image view that describes the placeholder.
261
298
public func placeholder( _ image: Image ) -> WebImage {
262
299
return placeholder {
263
- configure ( image: image)
300
+ configurations. reduce ( image) { ( previous, configuration) in
301
+ configuration ( previous)
302
+ }
264
303
}
265
304
}
266
305
0 commit comments