Bläddra i källkod

Merge pull request #1747 from onevcat/version6

Pick changes from version 6
Wei Wang 4 år sedan
förälder
incheckning
3aa11f2011

+ 5 - 1
Demo/Demo/Kingfisher-Demo/ViewControllers/TextAttachmentViewController.swift

@@ -49,7 +49,11 @@ class TextAttachmentViewController: UIViewController {
         KF.url(URL(string: "https://onevcat.com/assets/images/avatar.jpg")!)
             .resizing(referenceSize: CGSize(width: 30, height: 30))
             .roundCorner(radius: .point(15))
-            .set(to: textAttachment, attributedView: label)
+            .set(to: textAttachment, attributedView: self.getLabel())
+    }
+    
+    func getLabel() -> UILabel {
+        return label
     }
 }
 

+ 10 - 7
Sources/Extensions/NSTextAttachment+Kingfisher.swift

@@ -82,7 +82,7 @@ extension KingfisherWrapper where Base: NSTextAttachment {
     @discardableResult
     public func setImage(
         with source: Source?,
-        attributedView: KFCrossPlatformView,
+        attributedView: @autoclosure @escaping () -> KFCrossPlatformView,
         placeholder: KFCrossPlatformImage? = nil,
         options: KingfisherOptionsInfo? = nil,
         progressBlock: DownloadProgressBlock? = nil,
@@ -145,24 +145,26 @@ extension KingfisherWrapper where Base: NSTextAttachment {
     @discardableResult
     public func setImage(
         with resource: Resource?,
-        attributedView: KFCrossPlatformView,
+        attributedView: @autoclosure @escaping () -> KFCrossPlatformView,
         placeholder: KFCrossPlatformImage? = nil,
         options: KingfisherOptionsInfo? = nil,
         progressBlock: DownloadProgressBlock? = nil,
         completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
     {
+        let options = KingfisherParsedOptionsInfo(KingfisherManager.shared.defaultOptions + (options ?? .empty))
         return setImage(
             with: resource.map { .network($0) },
             attributedView: attributedView,
             placeholder: placeholder,
-            options: options,
+            parsedOptions: options,
             progressBlock: progressBlock,
-            completionHandler: completionHandler)
+            completionHandler: completionHandler
+        )
     }
 
     func setImage(
         with source: Source?,
-        attributedView: KFCrossPlatformView,
+        attributedView: @escaping () -> KFCrossPlatformView,
         placeholder: KFCrossPlatformImage? = nil,
         parsedOptions: KingfisherParsedOptionsInfo,
         progressBlock: DownloadProgressBlock? = nil,
@@ -222,10 +224,11 @@ extension KingfisherWrapper where Base: NSTextAttachment {
                     switch result {
                     case .success(let value):
                         self.base.image = value.image
+                        let view = attributedView()
                         #if canImport(UIKit)
-                        attributedView.setNeedsDisplay()
+                        view.setNeedsDisplay()
                         #else
-                        attributedView.setNeedsDisplay(attributedView.bounds)
+                        view.setNeedsDisplay(view.bounds)
                         #endif
                     case .failure:
                         if let image = options.onFailureImage {

+ 1 - 1
Sources/General/KF.swift

@@ -159,7 +159,7 @@ extension KF.Builder {
     /// - Returns: A task represents the image downloading, if initialized.
     ///            This value is `nil` if the image is being loaded from cache.
     @discardableResult
-    public func set(to attachment: NSTextAttachment, attributedView: KFCrossPlatformView) -> DownloadTask? {
+    public func set(to attachment: NSTextAttachment, attributedView: @autoclosure @escaping () -> KFCrossPlatformView) -> DownloadTask? {
         let placeholderImage = placeholder as? KFCrossPlatformImage ?? nil
         return attachment.kf.setImage(
             with: source,