Przeglądaj źródła

Merge branch 'dev2-kf数据迁移' into dev-v2-diyVideo
迁移 kf 的图片资源从 cache 到 document 永久保存

100Years 1 tydzień temu
rodzic
commit
6a1a2b8353

+ 4 - 0
AIEmoji.xcodeproj/project.pbxproj

@@ -196,6 +196,7 @@
 		A87587122D81702700286A66 /* TSUserDefaultData.swift in Sources */ = {isa = PBXBuildFile; fileRef = A87587102D81702700286A66 /* TSUserDefaultData.swift */; };
 		A87587162D81734300286A66 /* text_to_photo_style.json in Resources */ = {isa = PBXBuildFile; fileRef = A87587152D81733C00286A66 /* text_to_photo_style.json */; };
 		A87587182D81814500286A66 /* TSAIThinkingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A87587172D81812C00286A66 /* TSAIThinkingView.swift */; };
+		A880ECF22E420FDC00222C47 /* AppDelegate+KF.swift in Sources */ = {isa = PBXBuildFile; fileRef = A880ECF12E420FD600222C47 /* AppDelegate+KF.swift */; };
 		A88508882DBCD944000FBCEC /* MemoryLeakChecker.swift in Sources */ = {isa = PBXBuildFile; fileRef = A88508872DBCD940000FBCEC /* MemoryLeakChecker.swift */; };
 		A885088D2DBCE7BE000FBCEC /* TSPTPHistoryVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = A885088C2DBCE7BC000FBCEC /* TSPTPHistoryVC.swift */; };
 		A88508B62DBF142B000FBCEC /* TSGennertatorSelectStyleVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = A88508B52DBF142A000FBCEC /* TSGennertatorSelectStyleVC.swift */; };
@@ -599,6 +600,7 @@
 		A87587102D81702700286A66 /* TSUserDefaultData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TSUserDefaultData.swift; sourceTree = "<group>"; };
 		A87587152D81733C00286A66 /* text_to_photo_style.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = text_to_photo_style.json; sourceTree = "<group>"; };
 		A87587172D81812C00286A66 /* TSAIThinkingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TSAIThinkingView.swift; sourceTree = "<group>"; };
+		A880ECF12E420FD600222C47 /* AppDelegate+KF.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AppDelegate+KF.swift"; sourceTree = "<group>"; };
 		A88508872DBCD940000FBCEC /* MemoryLeakChecker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemoryLeakChecker.swift; sourceTree = "<group>"; };
 		A885088C2DBCE7BC000FBCEC /* TSPTPHistoryVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TSPTPHistoryVC.swift; sourceTree = "<group>"; };
 		A88508B52DBF142A000FBCEC /* TSGennertatorSelectStyleVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TSGennertatorSelectStyleVC.swift; sourceTree = "<group>"; };
@@ -2181,6 +2183,7 @@
 				A8F774922D38EA8C00AA6E93 /* Business */,
 				A8F774D82D38EA8C00AA6E93 /* Common */,
 				A8F774812D38E8B700AA6E93 /* AppDelegate.swift */,
+				A880ECF12E420FD600222C47 /* AppDelegate+KF.swift */,
 				A80E72712D40F86000C64288 /* TSLaunchVC.swift */,
 				A8F3D6492E2E2AC000DE6C9D /* launch2.png */,
 				A8F774822D38E8B700AA6E93 /* Assets.xcassets */,
@@ -3092,6 +3095,7 @@
 				A8BA76472DA4CC70000B6707 /* TSPTPSelectStyleView.swift in Sources */,
 				A8F7750A2D38EA8C00AA6E93 /* TSNetworkTool.swift in Sources */,
 				A8F7762D2D3A74A100AA6E93 /* TSGenmojiGennerateCell.swift in Sources */,
+				A880ECF22E420FDC00222C47 /* AppDelegate+KF.swift in Sources */,
 				605E20552DCC90CE0069F4B6 /* SchemeHandler.swift in Sources */,
 				A89EA6BC2D5DFB12000EB181 /* TSViewController.swift in Sources */,
 				A85E479D2D6809DC0018D62D /* TSBigIconBrowseCell.swift in Sources */,

+ 65 - 0
AIEmoji/AppDelegate+KF.swift

@@ -0,0 +1,65 @@
+//
+//  AppDelegate+KF.swift
+//  AIEmoji
+//
+//  Created by 100Years on 2025/8/5.
+//
+
+import Kingfisher
+extension AppDelegate {
+    //迁移 kf 的图片资源从 cache 到 document 永久保存
+    func handleKingfisher(){
+        moveCacheSmoothly()
+        let cache = try! ImageCache(
+            name: "default",
+            cacheDirectoryURL: FileManager.default.urls(for: .documentDirectory, in: .userDomainMask ).first!
+        )
+        cache.diskStorage.config.expiration = .never    // 永不过期
+        cache.diskStorage.config.sizeLimit = 0          // 无大小限制
+        KingfisherManager.shared.cache = cache
+    }
+    
+
+    func moveCacheSmoothly(){
+        // 1. 获取旧文件路径的图片
+        let fileManager = FileManager.default
+        guard let cachesURL = fileManager.urls(for: .cachesDirectory, in: .userDomainMask).first else {
+            return
+        }
+        
+        let permanent_image_cache = "com.onevcat.Kingfisher.ImageCache.default"
+        let srcURL = cachesURL.appendingPathComponent(permanent_image_cache)
+        guard fileManager.fileExists(atPath: srcURL.path),
+              TSFileManagerTool.hasContents(atPath: srcURL.path)
+        else {
+            dePrint("Cache Kingfisher 没有缓存,不用迁移")
+            return
+        }
+
+        //把srcURL 移动到新的 documentURL
+        guard let documentURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
+        let dstURL = documentURL.appendingPathComponent(permanent_image_cache)
+
+        // 4. 处理目标已存在的情况
+        if fileManager.fileExists(atPath: dstURL.path) {
+            do {
+                // 先删除目标文件夹(如果需要合并则改为遍历子文件)
+                try fileManager.removeItem(at: dstURL)
+                dePrint("已清理旧缓存目录")
+            } catch {
+                dePrint("清理旧缓存失败: \(error.localizedDescription)")
+                return
+            }
+        }
+
+        do {
+            dePrint("srcURL=\(srcURL)")
+            dePrint("dstURL=\(dstURL)")
+            try fileManager.moveItem(at: srcURL, to: dstURL)// 移动文件
+        } catch {
+            debugPrint("尝试移动文件失败: \(error.localizedDescription)")
+        }
+
+        dePrint("移动 Cache Kingfisher 到 library 流程结束")
+    }
+}

+ 1 - 0
AIEmoji/AppDelegate.swift

@@ -64,6 +64,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
     }
 
     func initPlatform() {
+//        handleKingfisher()//迁移 kf 的图片资源从 cache 到 document 永久保存
         TSCrashReporterTool.shared.setup()
         TSCrashReporterTool.shared.printCrashReports()
         TSColorConfigShared.naviMianTextColor = .white