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

This commit is contained in:
DreamPiggy 2020-04-15 17:31:44 +08:00
parent 0a41337ed0
commit d564aa5ca1
5 changed files with 35 additions and 8 deletions

View File

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

View File

@ -1379,7 +1379,7 @@
repositoryURL = "https://github.com/nalexn/ViewInspector.git";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 0.3.5;
minimumVersion = 0.3.11;
};
};
/* End XCRemoteSwiftPackageReference section */

View File

@ -6,8 +6,8 @@
"repositoryURL": "https://github.com/nalexn/ViewInspector.git",
"state": {
"branch": null,
"revision": "ec943ed718cd293b95f17a2b81e8917d6ed70752",
"version": "0.3.8"
"revision": "7d55eb940242512aad2bf28db354d09d5de43893",
"version": "0.3.11"
}
}
]

View File

@ -124,7 +124,6 @@ public struct WebImage : View {
/// Configure the platform image into the SwiftUI rendering image
func configure(image: PlatformImage) -> some View {
var image = image
// Actual rendering SwiftUI image
let result: Image
// NSImage works well with SwiftUI, include Animated and Vector Image
@ -133,6 +132,8 @@ public struct WebImage : View {
#else
// Fix the SwiftUI.Image rendering issue when use UIImage based, the `.aspectRatio` does not works. SwiftUI's Bug :)
// See issue #101
var image = image
var cgImage: CGImage?
// Case 1: UIAnimatedImage, grab poster image
if image.sd_isAnimated {
// check images property
@ -147,14 +148,21 @@ public struct WebImage : View {
// draw vector into bitmap with the screen scale (behavior like AppKit)
UIGraphicsBeginImageContextWithOptions(image.size, false, UIScreen.main.scale)
image.draw(at: .zero)
image = UIGraphicsGetImageFromCurrentImageContext() ?? .empty
cgImage = UIGraphicsGetImageFromCurrentImageContext()?.cgImage
UIGraphicsEndImageContext()
} else {
cgImage = image.cgImage
}
}
// Case 3: Image with EXIF orientation
if image.imageOrientation != .up {
cgImage = image.cgImage
}
// If we have CGImage, use CGImage based API, else use UIImage based API
if let cgImage = image.cgImage {
if let cgImage = cgImage {
let scale = image.scale
let orientation = image.imageOrientation.toSwiftUI
result = Image(decorative: cgImage, scale: image.scale, orientation: orientation)
result = Image(decorative: cgImage, scale: scale, orientation: orientation)
} else {
result = Image(uiImage: image)
}

View File

@ -133,4 +133,23 @@ class WebImageTests: XCTestCase {
ViewHosting.expel()
}
func testWebImageEXIFImage() throws {
let expectation = self.expectation(description: "WebImage EXIF image url")
// EXIF 2, Up Mirrored
let imageUrl = URL(string: "https://raw.githubusercontent.com/recurser/exif-orientation-examples/master/Landscape_2.jpg")
let imageView = WebImage(url: imageUrl)
let introspectView = imageView.onSuccess { image, cacheType in
let displayImage = try? imageView.inspect().group().image(0).cgImage()
let orientation = try! imageView.inspect().group().image(0).orientation()
XCTAssertNotNil(displayImage)
XCTAssertEqual(orientation, .upMirrored)
expectation.fulfill()
}.onFailure { error in
XCTFail(error.localizedDescription)
}
_ = try introspectView.inspect()
ViewHosting.host(view: introspectView)
self.waitForExpectations(timeout: 5, handler: nil)
ViewHosting.expel()
}
}