Browse Source

Move method

onevcat 4 years ago
parent
commit
d741585aff

+ 28 - 11
Sources/SwiftUI/KFImage.swift

@@ -29,25 +29,42 @@ import SwiftUI
 
 @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
 public struct KFImage: KFImageProtocol {
-    public typealias HoldingView = Image
-    public var context: Context<HoldingView>
+    public var context: Context<Image>
     public init(context: Context<Image>) {
         self.context = context
     }
 }
 
-// MARK: - Image compatibility.
 @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
-extension KFImage {
+extension Image: KFImageHoldingView {
+    public static func created(from image: KFCrossPlatformImage) -> Image {
+        if #available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *) {
+            return Image(crossPlatformImage: image)
+        } 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 = image.cgImage {
+                return Image(decorative: cgImage, scale: image.scale, orientation: image.imageOrientation.toSwiftUI())
+            } else {
+                return Image(crossPlatformImage: image)
+            }
+            #else
+            return Image(crossPlatformImage: image)
+            #endif
 
-    /// Configures current image with a `block`. This block will be lazily applied when creating the final `Image`.
-    /// - Parameter block: The block applies to loaded image.
-    /// - Returns: A `KFImage` view that configures internal `Image` with `block`.
-    public func configure(_ block: @escaping (Image) -> Image) -> KFImage {
-        var result = self
-        result.context.configurations.append(block)
-        return result
+        }
     }
+}
+
+// MARK: - Image compatibility.
+@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
+extension KFImage {
 
     public func resizable(
         capInsets: EdgeInsets = EdgeInsets(),

+ 9 - 2
Sources/SwiftUI/KFImageProtocol.swift

@@ -29,9 +29,7 @@ import SwiftUI
 
 @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
 public protocol KFImageProtocol: View, KFOptionSetter {
-    
     typealias Context = KFImage.Context<HoldingView>
-    
     associatedtype HoldingView: KFImageHoldingView
     var context: Context { get set }
     init(context: Context)
@@ -83,6 +81,15 @@ extension KFImageProtocol {
     init(binder: KFImage.ImageBinder) {
         self.init(context: KFImage.Context<HoldingView>(binder: binder))
     }
+    
+    /// Configures current image with a `block`. This block will be lazily applied when creating the final `Image`.
+    /// - Parameter block: The block applies to loaded image.
+    /// - Returns: A `KFImage` view that configures internal `Image` with `block`.
+    public func configure(_ block: @escaping (HoldingView) -> HoldingView) -> Self {
+        var result = self
+        result.context.configurations.append(block)
+        return result
+    }
 }
 
 @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)

+ 0 - 27
Sources/SwiftUI/KFImageRenderer.swift

@@ -88,33 +88,6 @@ struct KFImageRenderer<HoldingView> : View where HoldingView: KFImageHoldingView
     }
 }
 
-@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
-extension Image: KFImageHoldingView {
-    public static func created(from image: KFCrossPlatformImage) -> Image {
-        if #available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *) {
-            return Image(crossPlatformImage: image)
-        } 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 = image.cgImage {
-                return Image(decorative: cgImage, scale: image.scale, orientation: image.imageOrientation.toSwiftUI())
-            } else {
-                return Image(crossPlatformImage: image)
-            }
-            #else
-            return Image(crossPlatformImage: image)
-            #endif
-
-        }
-    }
-}
-
 @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
 extension Image {
     // Creates an Image with either UIImage or NSImage.