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

Refine API and prevent multiple completion invokes

onevcat 1 год назад
Родитель
Сommit
b7d33539c9

+ 7 - 1
Demo/Demo/Kingfisher-Demo/ViewControllers/LivePhotoViewController.swift

@@ -53,7 +53,13 @@ class LivePhotoViewController: UIViewController {
             "https://github.com/onevcat/Kingfisher-TestImages/raw/refs/heads/master/LivePhotos/live_photo_sample.MOV"
         ].compactMap(URL.init))
         livePhotoView.kf.setImage(with: source, completionHandler: { result in
-            print(result)
+            switch result {
+            case .success(let r):
+                print("Live Photo done. \(r.loadingInfo.cacheType)")
+                print("Info: \(String(describing: r.info))")
+            case .failure(let error):
+                print("Live Photo error: \(error)")
+            }
         })
     }
 }

+ 16 - 19
Sources/Extensions/PHLivePhotoView+Kingfisher.swift

@@ -71,21 +71,14 @@ extension KingfisherWrapper where Base: PHLivePhotoView {
     @discardableResult
     public func setImage(
         with source: LivePhotoSource?,
-        placeholder: KFCrossPlatformImage? = nil,
+        // placeholder: KFCrossPlatformImage? = nil, // Not supported yet
         options: KingfisherOptionsInfo? = nil,
         // progressBlock: DownloadProgressBlock? = nil, // Not supported yet
         completionHandler: (@MainActor @Sendable (Result<RetrieveLivePhotoResult, KingfisherError>) -> Void)? = nil
     ) -> Task<(), Never>? {
         var mutatingSelf = self
         guard let source = source else {
-            PHLivePhoto.request(
-                withResourceFileURLs: [],
-                placeholderImage: placeholder,
-                targetSize: .zero,
-                contentMode: .default
-            ) { photo, _ in
-                base.livePhoto = photo
-            }
+            base.livePhoto = nil
             mutatingSelf.taskIdentifier = nil
             completionHandler?(.failure(KingfisherError.imageSettingError(reason: .emptySource)))
             return nil
@@ -120,7 +113,7 @@ extension KingfisherWrapper where Base: PHLivePhotoView {
                 
                 PHLivePhoto.request(
                     withResourceFileURLs: loadingInfo.fileURLs,
-                    placeholderImage: placeholder,
+                    placeholderImage: nil,
                     targetSize: targetSize,
                     contentMode: contentMode,
                     resultHandler: { livePhoto, info in
@@ -149,16 +142,20 @@ extension KingfisherWrapper where Base: PHLivePhotoView {
                             return
                         }
                         
-                        if info.keys.contains(PHLivePhotoInfoCancelledKey) {
-                            let cancelled = (info[PHLivePhotoInfoCancelledKey] as? NSNumber)?.boolValue ?? false
-                            if cancelled {
-                                completionHandler?(.failure(
-                                    .requestError(reason: .livePhotoTaskCancelled(source: source)))
-                                )
-                                return
-                            }
+                        if (info[PHLivePhotoInfoCancelledKey] as? NSNumber)?.boolValue ?? false {
+                            completionHandler?(.failure(
+                                .requestError(reason: .livePhotoTaskCancelled(source: source)))
+                            )
+                            return
+                        }
+                        
+                        // If the PHLivePhotoInfoIsDegradedKey value in your result handler’s info dictionary is true,
+                        // Photos will call your result handler again.
+                        if (info[PHLivePhotoInfoIsDegradedKey] as? NSNumber)?.boolValue == true {
+                            // This makes `completionHandler` be only called once.
+                            return
                         }
-                            
+                        
                         completionHandler?(.success(result))
                     }
                 )