Skip to content

Fix the issue that using Image(uiImage:) will result wrong rendering mode in some component like TabBarItem, while using Image(decorative:scale:orientation:) works well #177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions SDWebImageSwiftUI/Classes/WebImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ public struct WebImage : View {
#if os(macOS)
result = Image(nsImage: image)
#else
// Fix the SwiftUI.Image rendering issue, like when use EXIF UIImage, the `.aspectRatio` does not works. SwiftUI's Bug :)
// See issue #101
// Fix the SwiftUI.Image rendering issue, like when use EXIF UIImage, the `.aspectRatio` does not works. SwiftUI's Bug :). See #101
// Always prefers `Image(decorative:scale:)` but not `Image(uiImage:scale:) to avoid bug on `TabbarItem`. See #175
var cgImage: CGImage?
// Case 1: Vector Image, draw bitmap image
if image.sd_isVector {
Expand All @@ -144,15 +144,8 @@ public struct WebImage : View {
cgImage = image.cgImage
}
} else {
// Case 2: Image with EXIF orientation (only EXIF 5-8 contains bug)
if [.left, .leftMirrored, .right, .rightMirrored].contains(image.imageOrientation) {
// Fixed by Apple in iOS 14+
if #available(iOS 14.0, watchOS 7.0, tvOS 14.0, *) {
// Do nothing
} else {
cgImage = image.cgImage
}
}
// Case 2: EXIF Image and Bitmap Image, prefers CGImage
cgImage = image.cgImage
}
// If we have CGImage, use CGImage based API, else use UIImage based API
if let cgImage = cgImage {
Expand Down
29 changes: 13 additions & 16 deletions Tests/WebImageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ class WebImageTests: XCTestCase {
let imageUrl = URL(string: "https://nr-platform.s3.amazonaws.com/uploads/platform/published_extension/branding_icon/275/AmazonS3.png")
let imageView = WebImage(url: imageUrl)
let introspectView = imageView.onSuccess { image, data, cacheType in
#if os(iOS) || os(tvOS)
let displayImage = try? imageView.inspect().group().image(0).uiImage()
#else
#if os(macOS)
let displayImage = try? imageView.inspect().group().image(0).nsImage()
#else
let displayImage = try? imageView.inspect().group().image(0).cgImage()
#endif
XCTAssertNotNil(displayImage)
expectation.fulfill()
Expand All @@ -46,15 +46,17 @@ class WebImageTests: XCTestCase {
let introspectView = imageView.onSuccess { image, data, cacheType in
if let animatedImage = image as? SDAnimatedImage {
XCTAssertTrue(imageView.isAnimating)
#if os(iOS) || os(tvOS)
let displayImage = try? imageView.inspect().group().image(0).uiImage()
#else
#if os(macOS)
let displayImage = try? imageView.inspect().group().image(0).nsImage()
let size = displayImage?.size
#else
let displayImage = try? imageView.inspect().group().image(0).cgImage()
let size = CGSize(width: displayImage?.width ?? 0, height: displayImage?.height ?? 0)
#endif
XCTAssertNotNil(displayImage)
// Check display image should match the animated poster frame
let posterImage = animatedImage.animatedImageFrame(at: 0)
XCTAssertEqual(displayImage?.size, posterImage?.size)
XCTAssertEqual(size, posterImage?.size)
expectation.fulfill()
} else {
XCTFail("WebImage animated image invalid")
Expand Down Expand Up @@ -162,15 +164,10 @@ class WebImageTests: XCTestCase {
let displayImage = try? imageView.inspect().group().image(0).nsImage()
XCTAssertNotNil(displayImage)
#else
if #available(iOS 14.0, watchOS 7.0, tvOS 14.0, *) {
let displayImage = try? imageView.inspect().group().image(0).uiImage()
XCTAssertEqual(displayImage, image)
} else {
let displayImage = try? imageView.inspect().group().image(0).cgImage()
let orientation = try? imageView.inspect().group().image(0).orientation()
XCTAssertNotNil(displayImage)
XCTAssertEqual(orientation, .leftMirrored)
}
let displayImage = try? imageView.inspect().group().image(0).cgImage()
let orientation = try? imageView.inspect().group().image(0).orientation()
XCTAssertNotNil(displayImage)
XCTAssertEqual(orientation, .leftMirrored)
#endif
expectation.fulfill()
}.onFailure { error in
Expand Down