TSImageStoreTool.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // TSImageStoreTool.swift
  3. // Pods
  4. //
  5. // Created by 100Years on 2025/6/16.
  6. //
  7. import Kingfisher
  8. public class TSImageStoreTool {
  9. public static func retrieveImageInMemoryCache(urlString: String) -> UIImage? {
  10. return ImageCache.default.retrieveImageInMemoryCache(forKey: urlString)
  11. }
  12. public static func storeImage(image:UIImage,urlString: String){
  13. if let url = URL(string: urlString){
  14. ImageCache.default.store(image, forKey: url.cacheKey)
  15. }
  16. }
  17. public static func removeImage(urlString: String){
  18. if urlString.isEmpty {
  19. return
  20. }
  21. if let url = URL(string: urlString){
  22. ImageCache.default.removeImage(forKey: url.cacheKey)
  23. }
  24. }
  25. public static func downloadImageWithProgress(
  26. urlString: String,
  27. progressHandler: ((Float) -> Void)? = nil,
  28. completion: @escaping (UIImage?) -> Void
  29. ) {
  30. // // 处理非HTTP URL的情况
  31. // if !urlString.contains("http") {
  32. // handleLocalImage(urlString: urlString, completion: completion)
  33. // return
  34. // }
  35. guard let url = URL(string: urlString) else {
  36. completion(nil)
  37. return
  38. }
  39. let cacheKey = url.cacheKey
  40. // 1. 首先尝试从内存缓存获取
  41. if let memoryImage = ImageCache.default.retrieveImageInMemoryCache(forKey: cacheKey) {
  42. completion(memoryImage)
  43. return
  44. }
  45. // 2. 然后尝试从磁盘缓存获取
  46. ImageCache.default.retrieveImage(forKey: cacheKey) { result in
  47. switch result {
  48. case .success(let value):
  49. if let diskImage = value.image {
  50. // 将从磁盘获取的图片存入内存缓存
  51. ImageCache.default.store(diskImage, forKey: cacheKey, toDisk: false)
  52. DispatchQueue.main.async {
  53. completion(diskImage)
  54. }
  55. return
  56. }
  57. // 3. 如果缓存中没有,则从网络下载
  58. downloadFromNetwork(url: url, cacheKey: cacheKey, progressHandler: progressHandler, completion: completion)
  59. case .failure:
  60. // 如果磁盘缓存获取失败,也尝试从网络下载
  61. downloadFromNetwork(url: url, cacheKey: cacheKey, progressHandler: progressHandler, completion: completion)
  62. }
  63. }
  64. }
  65. // private static func handleLocalImage(urlString: String, completion: @escaping (UIImage?) -> Void) {
  66. // if urlString.contains("/") {
  67. // completion(UIImage(contentsOfFile: urlString.fillCachePath))
  68. // } else {
  69. // completion(UIImage(named: urlString))
  70. // }
  71. // }
  72. private static func downloadFromNetwork(
  73. url: URL,
  74. cacheKey: String,
  75. progressHandler: ((Float) -> Void)?,
  76. completion: @escaping (UIImage?) -> Void
  77. ) {
  78. KingfisherManager.shared.retrieveImage(
  79. with: url,
  80. options: [],
  81. progressBlock: { receivedSize, totalSize in
  82. let progress = Float(receivedSize) / Float(totalSize)
  83. DispatchQueue.main.async {
  84. progressHandler?(progress)
  85. }
  86. },
  87. completionHandler: { result in
  88. DispatchQueue.main.async {
  89. switch result {
  90. case .success(let value):
  91. // 将下载的图片存入缓存
  92. ImageCache.default.store(value.image, forKey: cacheKey)
  93. completion(value.image)
  94. case .failure:
  95. completion(nil)
  96. }
  97. }
  98. }
  99. )
  100. }
  101. }