@@ -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,52 @@ 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+ // 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+
126168 // Should not use `EmptyView`, which does not respect to the container's frame modifier
127169 // Using a empty image instead for better compatible
128- configurations. reduce ( image ) { ( previous, configuration) in
170+ return configurations. reduce ( result ) { ( previous, configuration) in
129171 configuration ( previous)
130172 }
131173 }
@@ -136,12 +178,12 @@ public struct WebImage : View {
136178 if let placeholder = placeholder {
137179 // If use `.delayPlaceholder`, the placeholder is applied after loading failed, hide during loading :)
138180 if imageManager. options. contains ( . delayPlaceholder) && imageManager. isLoading {
139- return AnyView ( configure ( image: Image . empty) )
181+ return AnyView ( configure ( image: . empty) )
140182 } else {
141183 return placeholder
142184 }
143185 } else {
144- return AnyView ( configure ( image: Image . empty) )
186+ return AnyView ( configure ( image: . empty) )
145187 }
146188 }
147189
@@ -260,7 +302,9 @@ extension WebImage {
260302 /// - Parameter image: A Image view that describes the placeholder.
261303 public func placeholder( _ image: Image ) -> WebImage {
262304 return placeholder {
263- configure ( image: image)
305+ configurations. reduce ( image) { ( previous, configuration) in
306+ configuration ( previous)
307+ }
264308 }
265309 }
266310
0 commit comments