Skip to content

Commit e19c35a

Browse files
authored
Merge pull request #177 from SDWebImage/fix_image_sturct_from_uiimage
Fix the issue that using `Image(uiimage:)` will result wrong rendering mode in some component like `TabbarItem`, while using `Image(decorative:sclae:orientation:)` works well
2 parents 88f2d67 + f6074c2 commit e19c35a

File tree

2 files changed

+17
-27
lines changed

2 files changed

+17
-27
lines changed

Diff for: SDWebImageSwiftUI/Classes/WebImage.swift

+4-11
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ public struct WebImage : View {
123123
#if os(macOS)
124124
result = Image(nsImage: image)
125125
#else
126-
// Fix the SwiftUI.Image rendering issue, like when use EXIF UIImage, the `.aspectRatio` does not works. SwiftUI's Bug :)
127-
// See issue #101
126+
// Fix the SwiftUI.Image rendering issue, like when use EXIF UIImage, the `.aspectRatio` does not works. SwiftUI's Bug :). See #101
127+
// Always prefers `Image(decorative:scale:)` but not `Image(uiImage:scale:) to avoid bug on `TabbarItem`. See #175
128128
var cgImage: CGImage?
129129
// Case 1: Vector Image, draw bitmap image
130130
if image.sd_isVector {
@@ -144,15 +144,8 @@ public struct WebImage : View {
144144
cgImage = image.cgImage
145145
}
146146
} else {
147-
// Case 2: Image with EXIF orientation (only EXIF 5-8 contains bug)
148-
if [.left, .leftMirrored, .right, .rightMirrored].contains(image.imageOrientation) {
149-
// Fixed by Apple in iOS 14+
150-
if #available(iOS 14.0, watchOS 7.0, tvOS 14.0, *) {
151-
// Do nothing
152-
} else {
153-
cgImage = image.cgImage
154-
}
155-
}
147+
// Case 2: EXIF Image and Bitmap Image, prefers CGImage
148+
cgImage = image.cgImage
156149
}
157150
// If we have CGImage, use CGImage based API, else use UIImage based API
158151
if let cgImage = cgImage {

Diff for: Tests/WebImageTests.swift

+13-16
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ class WebImageTests: XCTestCase {
2222
let imageUrl = URL(string: "https://nr-platform.s3.amazonaws.com/uploads/platform/published_extension/branding_icon/275/AmazonS3.png")
2323
let imageView = WebImage(url: imageUrl)
2424
let introspectView = imageView.onSuccess { image, data, cacheType in
25-
#if os(iOS) || os(tvOS)
26-
let displayImage = try? imageView.inspect().group().image(0).uiImage()
27-
#else
25+
#if os(macOS)
2826
let displayImage = try? imageView.inspect().group().image(0).nsImage()
27+
#else
28+
let displayImage = try? imageView.inspect().group().image(0).cgImage()
2929
#endif
3030
XCTAssertNotNil(displayImage)
3131
expectation.fulfill()
@@ -46,15 +46,17 @@ class WebImageTests: XCTestCase {
4646
let introspectView = imageView.onSuccess { image, data, cacheType in
4747
if let animatedImage = image as? SDAnimatedImage {
4848
XCTAssertTrue(imageView.isAnimating)
49-
#if os(iOS) || os(tvOS)
50-
let displayImage = try? imageView.inspect().group().image(0).uiImage()
51-
#else
49+
#if os(macOS)
5250
let displayImage = try? imageView.inspect().group().image(0).nsImage()
51+
let size = displayImage?.size
52+
#else
53+
let displayImage = try? imageView.inspect().group().image(0).cgImage()
54+
let size = CGSize(width: displayImage?.width ?? 0, height: displayImage?.height ?? 0)
5355
#endif
5456
XCTAssertNotNil(displayImage)
5557
// Check display image should match the animated poster frame
5658
let posterImage = animatedImage.animatedImageFrame(at: 0)
57-
XCTAssertEqual(displayImage?.size, posterImage?.size)
59+
XCTAssertEqual(size, posterImage?.size)
5860
expectation.fulfill()
5961
} else {
6062
XCTFail("WebImage animated image invalid")
@@ -162,15 +164,10 @@ class WebImageTests: XCTestCase {
162164
let displayImage = try? imageView.inspect().group().image(0).nsImage()
163165
XCTAssertNotNil(displayImage)
164166
#else
165-
if #available(iOS 14.0, watchOS 7.0, tvOS 14.0, *) {
166-
let displayImage = try? imageView.inspect().group().image(0).uiImage()
167-
XCTAssertEqual(displayImage, image)
168-
} else {
169-
let displayImage = try? imageView.inspect().group().image(0).cgImage()
170-
let orientation = try? imageView.inspect().group().image(0).orientation()
171-
XCTAssertNotNil(displayImage)
172-
XCTAssertEqual(orientation, .leftMirrored)
173-
}
167+
let displayImage = try? imageView.inspect().group().image(0).cgImage()
168+
let orientation = try? imageView.inspect().group().image(0).orientation()
169+
XCTAssertNotNil(displayImage)
170+
XCTAssertEqual(orientation, .leftMirrored)
174171
#endif
175172
expectation.fulfill()
176173
}.onFailure { error in

0 commit comments

Comments
 (0)