Skip to content

Commit d564aa5

Browse files
committed
Only use CGImage based when the EXIF orientation is not equal to Up. I think SwfitUI will fix this issue in the future and we'd better use UIImage as much as possible
1 parent 0a41337 commit d564aa5

File tree

5 files changed

+35
-8
lines changed

5 files changed

+35
-8
lines changed

Example/SDWebImageSwiftUIDemo/ContentView.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ struct ContentView: View {
8181
"https://nr-platform.s3.amazonaws.com/uploads/platform/published_extension/branding_icon/275/AmazonS3.png",
8282
"https://raw.githubusercontent.com/ibireme/YYImage/master/Demo/YYImageDemo/mew_baseline.jpg",
8383
"https://via.placeholder.com/200x200.jpg",
84-
"https://dv6mh24acw2xi.cloudfront.net/public/moments/gabbr/tmpImg9143171451882065582.jpeg",
84+
"https://raw.githubusercontent.com/recurser/exif-orientation-examples/master/Landscape_2.jpg",
8585
"https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/w3c.svg",
8686
"https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/wikimedia.svg",
8787
"https://raw.githubusercontent.com/icons8/flat-color-icons/master/pdf/stack_of_photos.pdf",

SDWebImageSwiftUI.xcodeproj/project.pbxproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1379,7 +1379,7 @@
13791379
repositoryURL = "https://github.com/nalexn/ViewInspector.git";
13801380
requirement = {
13811381
kind = upToNextMajorVersion;
1382-
minimumVersion = 0.3.5;
1382+
minimumVersion = 0.3.11;
13831383
};
13841384
};
13851385
/* End XCRemoteSwiftPackageReference section */

SDWebImageSwiftUI.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"repositoryURL": "https://github.com/nalexn/ViewInspector.git",
77
"state": {
88
"branch": null,
9-
"revision": "ec943ed718cd293b95f17a2b81e8917d6ed70752",
10-
"version": "0.3.8"
9+
"revision": "7d55eb940242512aad2bf28db354d09d5de43893",
10+
"version": "0.3.11"
1111
}
1212
}
1313
]

SDWebImageSwiftUI/Classes/WebImage.swift

+12-4
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ public struct WebImage : View {
124124

125125
/// Configure the platform image into the SwiftUI rendering image
126126
func configure(image: PlatformImage) -> some View {
127-
var image = image
128127
// Actual rendering SwiftUI image
129128
let result: Image
130129
// NSImage works well with SwiftUI, include Animated and Vector Image
@@ -133,6 +132,8 @@ public struct WebImage : View {
133132
#else
134133
// Fix the SwiftUI.Image rendering issue when use UIImage based, the `.aspectRatio` does not works. SwiftUI's Bug :)
135134
// See issue #101
135+
var image = image
136+
var cgImage: CGImage?
136137
// Case 1: UIAnimatedImage, grab poster image
137138
if image.sd_isAnimated {
138139
// check images property
@@ -147,14 +148,21 @@ public struct WebImage : View {
147148
// draw vector into bitmap with the screen scale (behavior like AppKit)
148149
UIGraphicsBeginImageContextWithOptions(image.size, false, UIScreen.main.scale)
149150
image.draw(at: .zero)
150-
image = UIGraphicsGetImageFromCurrentImageContext() ?? .empty
151+
cgImage = UIGraphicsGetImageFromCurrentImageContext()?.cgImage
151152
UIGraphicsEndImageContext()
153+
} else {
154+
cgImage = image.cgImage
152155
}
153156
}
157+
// Case 3: Image with EXIF orientation
158+
if image.imageOrientation != .up {
159+
cgImage = image.cgImage
160+
}
154161
// If we have CGImage, use CGImage based API, else use UIImage based API
155-
if let cgImage = image.cgImage {
162+
if let cgImage = cgImage {
163+
let scale = image.scale
156164
let orientation = image.imageOrientation.toSwiftUI
157-
result = Image(decorative: cgImage, scale: image.scale, orientation: orientation)
165+
result = Image(decorative: cgImage, scale: scale, orientation: orientation)
158166
} else {
159167
result = Image(uiImage: image)
160168
}

Tests/WebImageTests.swift

+19
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,23 @@ class WebImageTests: XCTestCase {
133133
ViewHosting.expel()
134134
}
135135

136+
func testWebImageEXIFImage() throws {
137+
let expectation = self.expectation(description: "WebImage EXIF image url")
138+
// EXIF 2, Up Mirrored
139+
let imageUrl = URL(string: "https://raw.githubusercontent.com/recurser/exif-orientation-examples/master/Landscape_2.jpg")
140+
let imageView = WebImage(url: imageUrl)
141+
let introspectView = imageView.onSuccess { image, cacheType in
142+
let displayImage = try? imageView.inspect().group().image(0).cgImage()
143+
let orientation = try! imageView.inspect().group().image(0).orientation()
144+
XCTAssertNotNil(displayImage)
145+
XCTAssertEqual(orientation, .upMirrored)
146+
expectation.fulfill()
147+
}.onFailure { error in
148+
XCTFail(error.localizedDescription)
149+
}
150+
_ = try introspectView.inspect()
151+
ViewHosting.host(view: introspectView)
152+
self.waitForExpectations(timeout: 5, handler: nil)
153+
ViewHosting.expel()
154+
}
136155
}

0 commit comments

Comments
 (0)