Просмотр исходного кода

为其他扩展添加渐进式JPEG加载处理

lixiang1994 6 лет назад
Родитель
Сommit
70496bbf7e

+ 20 - 0
Sources/Extensions/NSButton+Kingfisher.swift

@@ -71,9 +71,29 @@ extension KingfisherWrapper where Base: NSButton {
         let issuedIdentifier = Source.Identifier.next()
         mutatingSelf.taskIdentifier = issuedIdentifier
 
+        let progressive = ImageProgressive(options)
+        
+        let dataUpdate = { (data: Data) in
+            guard
+                let data = progressive.scanning(data),
+                let callbacks = mutatingSelf.imageTask?.sessionTask.callbacks else {
+                return
+            }
+            progressive.decode(data, with: callbacks) { (image) in
+                guard mutatingSelf.imageTask != nil else { return }
+                
+                self.base.image = image
+            }
+        }
+        
         let task = KingfisherManager.shared.retrieveImage(
             with: source,
             options: options,
+            receivedBlock: { latest, received in
+                guard issuedIdentifier == self.taskIdentifier else { return }
+                
+                dataUpdate(received)
+            },
             progressBlock: { receivedSize, totalSize in
                 guard issuedIdentifier == self.taskIdentifier else { return }
                 progressBlock?(receivedSize, totalSize)

+ 21 - 0
Sources/Extensions/UIButton+Kingfisher.swift

@@ -70,9 +70,30 @@ extension KingfisherWrapper where Base: UIButton {
         var mutatingSelf = self
         let issuedTaskIdentifier = Source.Identifier.next()
         setTaskIdentifier(issuedTaskIdentifier, for: state)
+        
+        let progressive = ImageProgressive(options)
+        
+        let dataUpdate = { (data: Data) in
+            guard
+                let data = progressive.scanning(data),
+                let callbacks = mutatingSelf.imageTask?.sessionTask.callbacks else {
+                return
+            }
+            progressive.decode(data, with: callbacks) { (image) in
+                guard mutatingSelf.imageTask != nil else { return }
+                
+                self.base.setImage(image, for: state)
+            }
+        }
+        
         let task = KingfisherManager.shared.retrieveImage(
             with: source,
             options: options,
+            receivedBlock: { latest, received in
+                guard issuedTaskIdentifier == self.taskIdentifier(for: state) else { return }
+                
+                dataUpdate(received)
+            },
             progressBlock: { receivedSize, totalSize in
                 guard issuedTaskIdentifier == self.taskIdentifier(for: state) else { return }
                 progressBlock?(receivedSize, totalSize)

+ 21 - 0
Sources/Extensions/WKInterfaceImage+Kingfisher.swift

@@ -70,9 +70,30 @@ extension KingfisherWrapper where Base: WKInterfaceImage {
         
         let issuedTaskIdentifier = Source.Identifier.next()
         mutatingSelf.taskIdentifier = issuedTaskIdentifier
+        
+        let progressive = ImageProgressive(options)
+        
+        let dataUpdate = { (data: Data) in
+            guard
+                let data = progressive.scanning(data),
+                let callbacks = mutatingSelf.imageTask?.sessionTask.callbacks else {
+                    return
+            }
+            progressive.decode(data, with: callbacks) { (image) in
+                guard mutatingSelf.imageTask != nil else { return }
+                
+                self.base.setImage(image)
+            }
+        }
+        
         let task = KingfisherManager.shared.retrieveImage(
             with: source,
             options: options,
+            receivedBlock: { latest, received in
+                guard issuedTaskIdentifier == self.taskIdentifier else { return }
+                
+                dataUpdate(received)
+            },
             progressBlock: { receivedSize, totalSize in
                 guard issuedTaskIdentifier == self.taskIdentifier else { return }
                 progressBlock?(receivedSize, totalSize)

+ 2 - 1
Sources/Image/ImageProgressive.swift

@@ -24,7 +24,8 @@
 //  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 //  THE SOFTWARE.
 
-import UIKit
+import Foundation
+import CoreGraphics
 
 private let sharedProcessingQueue: CallbackQueue =
     .dispatch(DispatchQueue(label: "com.onevcat.Kingfisher.ImageDownloader.Process"))