@@ -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,52 @@ 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
+ // Actual rendering SwiftUI image
128
+ let result : Image
129
+ // NSImage works well with SwiftUI, include Vector and EXIF images.
130
+ #if os(macOS)
131
+ result = Image ( nsImage: image)
132
+ #else
133
+ // Fix the SwiftUI.Image rendering issue, like when use EXIF UIImage, the `.aspectRatio` does not works. SwiftUI's Bug :)
134
+ // See issue #101
135
+ var cgImage : CGImage ?
136
+ // Case 1: Vector Image, draw bitmap image
137
+ if image. sd_isVector {
138
+ // ensure CGImage is nil
139
+ if image. cgImage == nil {
140
+ // draw vector into bitmap with the screen scale (behavior like AppKit)
141
+ #if os(iOS) || os(tvOS)
142
+ let scale = UIScreen . main. scale
143
+ #else
144
+ let scale = WKInterfaceDevice . current ( ) . screenScale
145
+ #endif
146
+ UIGraphicsBeginImageContextWithOptions ( image. size, false , scale)
147
+ image. draw ( at: . zero)
148
+ cgImage = UIGraphicsGetImageFromCurrentImageContext ( ) ? . cgImage
149
+ UIGraphicsEndImageContext ( )
150
+ } else {
151
+ cgImage = image. cgImage
152
+ }
153
+ }
154
+ // Case 2: Image with EXIF orientation (only EXIF 5-8 contains bug)
155
+ else if [ . left, . leftMirrored, . right, . rightMirrored] . contains ( image. imageOrientation) {
156
+ cgImage = image. cgImage
157
+ }
158
+ // If we have CGImage, use CGImage based API, else use UIImage based API
159
+ if let cgImage = cgImage {
160
+ let scale = image. scale
161
+ let orientation = image. imageOrientation. toSwiftUI
162
+ result = Image ( decorative: cgImage, scale: scale, orientation: orientation)
163
+ } else {
164
+ result = Image ( uiImage: image)
165
+ }
166
+ #endif
167
+
126
168
// Should not use `EmptyView`, which does not respect to the container's frame modifier
127
169
// Using a empty image instead for better compatible
128
- configurations. reduce ( image ) { ( previous, configuration) in
170
+ return configurations. reduce ( result ) { ( previous, configuration) in
129
171
configuration ( previous)
130
172
}
131
173
}
@@ -136,12 +178,12 @@ public struct WebImage : View {
136
178
if let placeholder = placeholder {
137
179
// If use `.delayPlaceholder`, the placeholder is applied after loading failed, hide during loading :)
138
180
if imageManager. options. contains ( . delayPlaceholder) && imageManager. isLoading {
139
- return AnyView ( configure ( image: Image . empty) )
181
+ return AnyView ( configure ( image: . empty) )
140
182
} else {
141
183
return placeholder
142
184
}
143
185
} else {
144
- return AnyView ( configure ( image: Image . empty) )
186
+ return AnyView ( configure ( image: . empty) )
145
187
}
146
188
}
147
189
@@ -260,7 +302,9 @@ extension WebImage {
260
302
/// - Parameter image: A Image view that describes the placeholder.
261
303
public func placeholder( _ image: Image ) -> WebImage {
262
304
return placeholder {
263
- configure ( image: image)
305
+ configurations. reduce ( image) { ( previous, configuration) in
306
+ configuration ( previous)
307
+ }
264
308
}
265
309
}
266
310
0 commit comments