onevcat 7 лет назад
Родитель
Сommit
ebc9ff741c

+ 1 - 1
Sources/Extensions/UIButton+Kingfisher.swift

@@ -266,6 +266,6 @@ extension KingfisherClass where Base: UIButton {
     
     
     private var backgroundImageTask: DownloadTask? {
     private var backgroundImageTask: DownloadTask? {
         get { return getAssociatedObject(base, &backgroundImageTaskKey) }
         get { return getAssociatedObject(base, &backgroundImageTaskKey) }
-        set { setAssignAssociatedObject(base, &backgroundImageTaskKey, newValue) }
+        set { setRetainedAssociatedObject(base, &backgroundImageTaskKey, newValue) }
     }
     }
 }
 }

+ 2 - 10
Sources/Image/ImageDrawing.swift

@@ -201,16 +201,8 @@ extension KingfisherClass where Base: Image {
     /// - Note: This method only works for CG-based image. The current image scale is kept.
     /// - Note: This method only works for CG-based image. The current image scale is kept.
     ///         For any non-CG-based image, `base` itself is returned.
     ///         For any non-CG-based image, `base` itself is returned.
     public func resize(to size: CGSize, for contentMode: ContentMode) -> Image {
     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)
-        case .none:
-            return resize(to: size)
-        }
+        let newSize = size.kf.resize(to: size, for: contentMode)
+        return resize(to: newSize)
     }
     }
     
     
     /// Crops `base` image to a new size with a given anchor.
     /// Crops `base` image to a new size with a given anchor.

+ 5 - 0
Sources/Utility/CallbackQueue.swift

@@ -34,9 +34,14 @@ import Foundation
 /// - untouch: Do not change the calling queue for closure.
 /// - untouch: Do not change the calling queue for closure.
 /// - dispatch: Dispatches to a specified `DispatchQueue`.
 /// - dispatch: Dispatches to a specified `DispatchQueue`.
 public enum CallbackQueue {
 public enum CallbackQueue {
+    /// Dispatch the calling to `DispatchQueue.main` with an `async` behavior.
     case mainAsync
     case mainAsync
+    /// Dispatch the calling to `DispatchQueue.main` with an `async` behavior if current queue is not
+    /// `.main`. Otherwise, call the closure immediately in current main queue.
     case mainCurrentOrAsync
     case mainCurrentOrAsync
+    /// Do not change the calling queue for closure.
     case untouch
     case untouch
+    /// Dispatches to a specified `DispatchQueue`.
     case dispatch(DispatchQueue)
     case dispatch(DispatchQueue)
     
     
     public func execute(_ block: @escaping () -> Void) {
     public func execute(_ block: @escaping () -> Void) {

+ 0 - 4
Sources/Utility/Runtime.swift

@@ -33,7 +33,3 @@ func getAssociatedObject<T>(_ object: Any, _ key: UnsafeRawPointer) -> T? {
 func setRetainedAssociatedObject<T>(_ object: Any, _ key: UnsafeRawPointer, _ value: T) {
 func setRetainedAssociatedObject<T>(_ object: Any, _ key: UnsafeRawPointer, _ value: T) {
     objc_setAssociatedObject(object, key, value, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
     objc_setAssociatedObject(object, key, value, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
 }
 }
-
-func setAssignAssociatedObject<T>(_ object: Any, _ key: UnsafeRawPointer, _ value: T) {
-    objc_setAssociatedObject(object, key, value, .OBJC_ASSOCIATION_ASSIGN)
-}

+ 21 - 1
Sources/Utility/SizeExtensions.swift

@@ -29,6 +29,12 @@ import CoreGraphics
 extension CGSize: KingfisherStructCompatible {}
 extension CGSize: KingfisherStructCompatible {}
 extension KingfisherStruct where Base == CGSize {
 extension KingfisherStruct where Base == CGSize {
     
     
+    /// Returns a size by resizing the `base` size to a target size under a given content mode.
+    ///
+    /// - Parameters:
+    ///   - size: The target size to resize to.
+    ///   - contentMode: Content mode of the target size should be when resizing.
+    /// - Returns: The resized size under the given `ContentMode`.
     public func resize(to size: CGSize, for contentMode: ContentMode) -> CGSize {
     public func resize(to size: CGSize, for contentMode: ContentMode) -> CGSize {
         switch contentMode {
         switch contentMode {
         case .aspectFit:
         case .aspectFit:
@@ -36,10 +42,14 @@ extension KingfisherStruct where Base == CGSize {
         case .aspectFill:
         case .aspectFill:
             return filling(size)
             return filling(size)
         case .none:
         case .none:
-            return base
+            return size
         }
         }
     }
     }
     
     
+    /// Returns a size by resizing the `base` size by making it aspect fitting the given `size`.
+    ///
+    /// - Parameter size: The size in which the `base` should fit in.
+    /// - Returns: The size fitted in by the input `size`, while keeps `base` aspect.
     public func constrained(_ size: CGSize) -> CGSize {
     public func constrained(_ size: CGSize) -> CGSize {
         let aspectWidth = round(aspectRatio * size.height)
         let aspectWidth = round(aspectRatio * size.height)
         let aspectHeight = round(size.width / aspectRatio)
         let aspectHeight = round(size.width / aspectRatio)
@@ -49,6 +59,10 @@ extension KingfisherStruct where Base == CGSize {
             CGSize(width: aspectWidth, height: size.height)
             CGSize(width: aspectWidth, height: size.height)
     }
     }
     
     
+    /// Returns a size by resizing the `base` size by making it aspect filling the given `size`.
+    ///
+    /// - Parameter size: The size in which the `base` should fill.
+    /// - Returns: The size be filled by the input `size`, while keeps `base` aspect.
     public func filling(_ size: CGSize) -> CGSize {
     public func filling(_ size: CGSize) -> CGSize {
         let aspectWidth = round(aspectRatio * size.height)
         let aspectWidth = round(aspectRatio * size.height)
         let aspectHeight = round(size.width / aspectRatio)
         let aspectHeight = round(size.width / aspectRatio)
@@ -58,6 +72,12 @@ extension KingfisherStruct where Base == CGSize {
             CGSize(width: aspectWidth, height: size.height)
             CGSize(width: aspectWidth, height: size.height)
     }
     }
     
     
+    /// Returns a `CGRect` for which the `base` size is constrained to an input `size` at a given `anchor` point.
+    ///
+    /// - Parameters:
+    ///   - size: The size in which the `base` should be constrainted to.
+    ///   - anchor: An anchor point in which the size constrainting should happen.
+    /// - Returns: The result `CGRect` for the constrainting operation.
     public func constrainedRect(for size: CGSize, anchor: CGPoint) -> CGRect {
     public func constrainedRect(for size: CGSize, anchor: CGPoint) -> CGRect {
         
         
         let unifiedAnchor = CGPoint(x: anchor.x.clamped(to: 0.0...1.0),
         let unifiedAnchor = CGPoint(x: anchor.x.clamped(to: 0.0...1.0),