Przeglądaj źródła

Allow fit/fill scaling for KFAnimatedImage

onevcat 4 lat temu
rodzic
commit
26a9f4b797

+ 1 - 0
Demo/Demo/Kingfisher-Demo/SwiftUIViews/AnimatedImageDemo.swift

@@ -52,6 +52,7 @@ struct AnimatedImageDemo: View {
                 }
                 .fade(duration: 1)
                 .forceTransition()
+                .aspectRatio(contentMode: .fill)
                 .frame(width: 300, height: 300)
                 .cornerRadius(20)
                 .shadow(radius: 5)

+ 2 - 0
Sources/SwiftUI/ImageContext.swift

@@ -41,10 +41,12 @@ extension KFImage {
     }
 }
 
+#if canImport(UIKit)
 @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
 extension KFAnimatedImage {
     public typealias Context = KFImage.Context
     typealias ImageBinder = KFImage.ImageBinder
 }
+#endif
 
 #endif

+ 5 - 1
Sources/SwiftUI/KFAnimatedImage.swift

@@ -24,7 +24,7 @@
 //  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 //  THE SOFTWARE.
 
-#if canImport(SwiftUI) && canImport(Combine)
+#if canImport(SwiftUI) && canImport(UIKit)
 import SwiftUI
 
 @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
@@ -48,6 +48,10 @@ public struct KFAnimatedImageViewRepresenter: UIViewRepresentable, KFImageHoldin
     public func makeUIView(context: Context) -> AnimatedImageView {
         let view = AnimatedImageView()
         view.image = image
+        
+        // Allow SwiftUI scale (fit/fill) working fine.
+        view.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
+        view.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
         return view
     }