onevcat 9 лет назад
Родитель
Сommit
9b8e42e1b4
2 измененных файлов с 34 добавлено и 23 удалено
  1. 21 5
      Sources/Image.swift
  2. 13 18
      Sources/ImageProcessor.swift

+ 21 - 5
Sources/Image.swift

@@ -377,11 +377,9 @@ extension Kingfisher where Base: Image {
     func resize(to size: CGSize, for contentMode: UIViewContentMode) -> Image {
         switch contentMode {
         case .scaleAspectFit:
-            let newSize = self.size.kf.constrained(size)
-            return resize(to: newSize)
+            return resize(to: size, for: .aspectFit)
         case .scaleAspectFill:
-            let newSize = self.size.kf.filling(size)
-            return resize(to: newSize)
+            return resize(to: size, for: .aspectFill)
         default:
             return resize(to: size)
         }
@@ -389,7 +387,6 @@ extension Kingfisher where Base: Image {
     #endif
     
     // MARK: - Resize
-    
     /// Resize `self` to an image of new size.
     ///
     /// - parameter size: The target size.
@@ -414,6 +411,25 @@ extension Kingfisher where Base: Image {
         }
     }
     
+    /// Resize `self` to an image of new size, respecting the content mode.
+    ///
+    /// - Parameters:
+    ///   - size: The target size.
+    ///   - contentMode: Content mode of output image should be.
+    /// - Returns: An image with new size.
+    public func resize(to size: CGSize, for contentMode: ContentMode) -> Image {
+        switch contentMode {
+        case .aspectFit:
+            let newSize = self.size.kf.constrained(size)
+            return resize(to: newSize)
+        case .aspectFill:
+            let newSize = self.size.kf.filling(size)
+            return resize(to: newSize)
+        default:
+            return resize(to: size)
+        }
+    }
+    
     // MARK: - Blur
     
     /// Create an image with blur effect based on `self`.

+ 13 - 18
Sources/ImageProcessor.swift

@@ -165,14 +165,20 @@ public struct RoundCornerImageProcessor: ImageProcessor {
     }
 }
 
+
+/// Specify how a size adjusts itself to fit a target.
+///
+/// - none: Not scale the content.
+/// - aspectFit: Scale the content to fit the size of the view by maintaining the aspect ratio.
+/// - aspectFill: Scale the content to fill the size of the view
+public enum ContentMode {
+    case none
+    case aspectFit
+    case aspectFill
+}
+
 /// Processor for resizing images. Only CG-based images are supported in macOS.
 public struct ResizingImageProcessor: ImageProcessor {
-    public enum ContentMode {
-        case none
-        case aspectFit
-        case aspectFill
-    }
-    
     public let identifier: String
     
     /// Target size of output image should be.
@@ -202,18 +208,7 @@ public struct ResizingImageProcessor: ImageProcessor {
     public func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image? {
         switch item {
         case .image(let image):
-            var size: CGSize
-            
-            switch targetContentMode {
-            case .none:
-                size = targetSize
-            case .aspectFill:
-                size = image.size.kf.filling(targetSize)
-            case .aspectFit:
-                size = image.size.kf.constrained(targetSize)
-            }
-            
-            return image.kf.resize(to: size)
+            return image.kf.resize(to: targetSize, for: targetContentMode)
         case .data(_):
             return (DefaultImageProcessor.default >> self).process(item: item, options: options)
         }