Sfoglia il codice sorgente

Only apply orientation fix for iOS 13

onevcat 5 anni fa
parent
commit
cdd45f7f10

+ 2 - 2
Demo/Demo/Kingfisher-macOS-Demo/ViewController.swift

@@ -60,8 +60,8 @@ extension ViewController: NSCollectionViewDataSource {
         item.imageView?.kf.indicatorType = .activity
         KF.url(url)
             .roundCorner(radius: .point(20))
-            .progress { receivedSize, totalSize in print("\(indexPath.item + 1): \(receivedSize)/\(totalSize)") }
-            .done { print($0) }
+            .onProgress { receivedSize, totalSize in print("\(indexPath.item + 1): \(receivedSize)/\(totalSize)") }
+            .onSuccess { print($0) }
             .set(to: item.imageView!)
         
         // Set imageView's `animates` to true if you are loading a GIF.

+ 1 - 8
Sources/SwiftUI/ImageBinder.swift

@@ -99,15 +99,8 @@ extension KFImage {
                         self.downloadTask = nil
                         switch result {
                         case .success(let value):
-
-                            // The normalized version of image is used to solve #1395
-                            // It should be not necessary if SwiftUI.Image can handle resizing correctly when created
-                            // by `Image.init(uiImage:)`. (The orientation information should be already contained in
-                            // a `UIImage`)
-                            // https://github.com/onevcat/Kingfisher/issues/1395
-                            let image = value.image.kf.normalized
                             let r = RetrieveImageResult(
-                                image: image, cacheType: value.cacheType, source: value.source, originalSource: value.originalSource
+                                image: value.image, cacheType: value.cacheType, source: value.source, originalSource: value.originalSource
                             )
                             CallbackQueue.mainCurrentOrAsync.execute {
                                 done(.success(r))

+ 44 - 1
Sources/SwiftUI/KFImage.swift

@@ -152,7 +152,7 @@ struct KFImageRenderer: View {
     var body: some View {
         if case .success(let r) = loadingResult {
             configurations
-                .reduce(Image(crossPlatformImage: r.image)) {
+                .reduce(imageFromResult(r.image)) {
                     current, config in config(current)
                 }
                 .opacity(isLoaded ? 1.0 : 0.0)
@@ -193,6 +193,30 @@ struct KFImageRenderer: View {
         }
     }
 
+    private func imageFromResult(_ resultImage: KFCrossPlatformImage) -> Image {
+        if #available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *) {
+            return Image(crossPlatformImage: resultImage)
+        } else {
+            #if canImport(UIKit)
+            // The CG image is used to solve #1395
+            // It should be not necessary if SwiftUI.Image can handle resizing correctly when created
+            // by `Image.init(uiImage:)`. (The orientation information should be already contained in
+            // a `UIImage`)
+            // https://github.com/onevcat/Kingfisher/issues/1395
+            //
+            // This issue happens on iOS 13 and was fixed by Apple from iOS 14.
+            if let cgImage = resultImage.cgImage {
+                return Image(decorative: cgImage, scale: resultImage.scale, orientation: resultImage.imageOrientation.toSwiftUI())
+            } else {
+                return Image(crossPlatformImage: resultImage)
+            }
+            #else
+            return Image(crossPlatformImage: resultImage)
+            #endif
+
+        }
+    }
+
     private func shouldApplyFade(cacheType: CacheType) -> Bool {
         binder.options.forceTransition || cacheType == .none
     }
@@ -216,6 +240,25 @@ extension ImageTransition {
     }
 }
 
+#if canImport(UIKit)
+@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
+extension UIImage.Orientation {
+    func toSwiftUI() -> Image.Orientation {
+        switch self {
+        case .down: return .down
+        case .up: return .up
+        case .left: return .left
+        case .right: return .right
+        case .upMirrored: return .upMirrored
+        case .downMirrored: return .downMirrored
+        case .leftMirrored: return .leftMirrored
+        case .rightMirrored: return .rightMirrored
+        @unknown default: return .up
+        }
+    }
+}
+#endif
+
 // MARK: - Image compatibility.
 @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
 extension KFImage {