Browse Source

Merge pull request #1746 from onevcat/feature/delayed-attribute-view

Make text attachment attribute view a closure
Wei Wang 4 years ago
parent
commit
8dabf57b33

+ 1 - 1
Demo/Demo/Kingfisher-Demo/SwiftUIViews/MainView.swift

@@ -2,7 +2,7 @@
 //  MainView.swift
 //  Kingfisher
 //
-//  Created by jp20028 on 2019/08/07.
+//  Created by Wei Wang on 2019/08/07.
 //
 //  Copyright (c) 2019 Wei Wang <onevcat@gmail.com>
 //

+ 1 - 1
Demo/Demo/Kingfisher-Demo/ViewControllers/AutoSizingTableViewController.swift

@@ -2,7 +2,7 @@
 //  AutoSizingTableViewController.swift
 //  Kingfisher
 //
-//  Created by JP20028 on 2021/03/15.
+//  Created by Wei Wang on 2021/03/15.
 //
 //  Copyright (c) 2021 Wei Wang <onevcat@gmail.com>
 //

+ 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
     }
 }
 

+ 5 - 4
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,
@@ -162,7 +162,7 @@ extension KingfisherWrapper where Base: NSTextAttachment {
 
     func setImage(
         with source: Source?,
-        attributedView: KFCrossPlatformView,
+        attributedView: @escaping () -> KFCrossPlatformView,
         placeholder: KFCrossPlatformImage? = nil,
         parsedOptions: KingfisherParsedOptionsInfo,
         progressBlock: DownloadProgressBlock? = nil,
@@ -222,10 +222,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,