@@ -63,13 +63,13 @@ public struct WebImage : View {
6363 // this prefetch the memory cache of image, to immediately render it on screen
6464 // this solve the case when `onAppear` not been called, for example, some transaction indeterminate state, SwiftUI :)
6565 if imageManager. isFirstPrefetch {
66- self . imageManager. prefetch ( )
66+ imageManager. prefetch ( )
6767 }
6868 return Group {
6969 if imageManager. image != nil {
70- if isAnimating && !self . imageManager. isIncremental {
70+ if isAnimating && !imageManager. isIncremental {
7171 if currentFrame != nil {
72- configure ( image: Image ( platformImage : currentFrame!) )
72+ configure ( image: currentFrame!)
7373 . onAppear {
7474 self . imagePlayer? . startPlaying ( )
7575 }
@@ -84,16 +84,16 @@ public struct WebImage : View {
8484 }
8585 }
8686 } else {
87- configure ( image: Image ( platformImage : imageManager. image!) )
87+ configure ( image: imageManager. image!)
8888 . onReceive ( imageManager. $image) { image in
8989 self . setupPlayer ( image: image)
9090 }
9191 }
9292 } else {
9393 if currentFrame != nil {
94- configure ( image: Image ( platformImage : currentFrame!) )
94+ configure ( image: currentFrame!)
9595 } else {
96- configure ( image: Image ( platformImage : imageManager. image!) )
96+ configure ( image: imageManager. image!)
9797 }
9898 }
9999 } else {
@@ -122,10 +122,47 @@ public struct WebImage : View {
122122 }
123123 }
124124
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+
126163 // Should not use `EmptyView`, which does not respect to the container's frame modifier
127164 // Using a empty image instead for better compatible
128- configurations. reduce ( image ) { ( previous, configuration) in
165+ return configurations. reduce ( result ) { ( previous, configuration) in
129166 configuration ( previous)
130167 }
131168 }
@@ -136,12 +173,12 @@ public struct WebImage : View {
136173 if let placeholder = placeholder {
137174 // If use `.delayPlaceholder`, the placeholder is applied after loading failed, hide during loading :)
138175 if imageManager. options. contains ( . delayPlaceholder) && imageManager. isLoading {
139- return AnyView ( configure ( image: Image . empty) )
176+ return AnyView ( configure ( image: . empty) )
140177 } else {
141178 return placeholder
142179 }
143180 } else {
144- return AnyView ( configure ( image: Image . empty) )
181+ return AnyView ( configure ( image: . empty) )
145182 }
146183 }
147184
@@ -260,7 +297,9 @@ extension WebImage {
260297 /// - Parameter image: A Image view that describes the placeholder.
261298 public func placeholder( _ image: Image ) -> WebImage {
262299 return placeholder {
263- configure ( image: image)
300+ configurations. reduce ( image) { ( previous, configuration) in
301+ configuration ( previous)
302+ }
264303 }
265304 }
266305
0 commit comments