2
0

ImageProgressive.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. //
  2. // ImageProgressive.swift
  3. // Kingfisher
  4. //
  5. // Created by lixiang on 2019/5/10.
  6. //
  7. // Copyright (c) 2019 Wei Wang <onevcat@gmail.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. import Foundation
  27. import CoreGraphics
  28. #if os(macOS)
  29. import AppKit
  30. #else
  31. import UIKit
  32. #endif
  33. private let sharedProcessingQueue: CallbackQueue =
  34. .dispatch(DispatchQueue(label: "com.onevcat.Kingfisher.ImageDownloader.Process"))
  35. /// Represents a progressive loading for images which supports this feature.
  36. public struct ImageProgressive: Sendable {
  37. /// The updating strategy when an intermediate progressive image is generated and about to be set to the hosting view.
  38. public enum UpdatingStrategy {
  39. /// Use the progressive image as it is.
  40. ///
  41. /// > It is the standard behavior when handling the progressive image.
  42. case `default`
  43. /// Discard this progressive image and keep the current displayed one.
  44. case keepCurrent
  45. /// Replace the image to a new one.
  46. ///
  47. /// If the progressive loading is initialized by a view extension in Kingfisher, the replacing image will be
  48. /// used to update the view.
  49. case replace(KFCrossPlatformImage?)
  50. }
  51. /// A default `ImageProgressive` could be used across. It blurs the progressive loading with the fastest
  52. /// scan enabled and scan interval as 0.
  53. @available(*, deprecated, message: "Getting a default `ImageProgressive` is deprecated due to its syntax semantic is not clear. Use `ImageProgressive.init` instead.", renamed: "init()")
  54. public static let `default` = ImageProgressive(
  55. isBlur: true,
  56. isFastestScan: true,
  57. scanInterval: 0
  58. )
  59. /// Indicates whether to enable blur effect processing.
  60. public var isBlur: Bool
  61. /// Indicates whether to enable the fastest scan.
  62. public var isFastestScan: Bool
  63. /// The minimum time interval for each scan.
  64. public var scanInterval: TimeInterval
  65. /// Called when an intermediate image is prepared and about to be set to the image view.
  66. ///
  67. /// If implemented, you should return an ``UpdatingStrategy`` value from this delegate. This value will be used to
  68. /// update the hosting view, if any. Otherwise, if there is no hosting view (i.e., the image retrieval is not
  69. /// happening from a view extension method), the returned ``UpdatingStrategy`` is ignored.
  70. public let onImageUpdated = Delegate<KFCrossPlatformImage, UpdatingStrategy>()
  71. /// Creates an `ImageProgressive` value with default settings.
  72. ///
  73. /// It enables progressive loading with the fastest scan enabled and a scan interval of 0, resulting in a blurred
  74. /// effect.
  75. public init() {
  76. self.init(isBlur: true, isFastestScan: true, scanInterval: 0)
  77. }
  78. /// Creates an `ImageProgressive` value with the given values.
  79. ///
  80. /// - Parameters:
  81. /// - isBlur: Indicates whether to enable blur effect processing.
  82. /// - isFastestScan: Indicates whether to enable the fastest scan.
  83. /// - scanInterval: The minimum time interval for each scan.
  84. public init(
  85. isBlur: Bool,
  86. isFastestScan: Bool,
  87. scanInterval: TimeInterval
  88. )
  89. {
  90. self.isBlur = isBlur
  91. self.isFastestScan = isFastestScan
  92. self.scanInterval = scanInterval
  93. }
  94. }
  95. // A data receiving provider to update the image. Working with an `ImageProgressive`, it helps to implement the image
  96. // progressive effect.
  97. final class ImageProgressiveProvider: DataReceivingSideEffect, @unchecked Sendable {
  98. private let propertyQueue = DispatchQueue(label: "com.onevcat.Kingfisher.ImageProgressiveProviderPropertyQueue")
  99. private var _onShouldApply: () -> Bool = { return true }
  100. var onShouldApply: () -> Bool {
  101. get { propertyQueue.sync { _onShouldApply } }
  102. set { propertyQueue.sync { _onShouldApply = newValue } }
  103. }
  104. func onDataReceived(_ session: URLSession, task: SessionDataTask, data: Data) {
  105. DispatchQueue.main.async {
  106. guard self.onShouldApply() else { return }
  107. self.update(data: task.mutableData, with: task.callbacks)
  108. }
  109. }
  110. private let progressive: ImageProgressive
  111. private let refresh: (KFCrossPlatformImage) -> Void
  112. private let decoder: ImageProgressiveDecoder
  113. private let queue = ImageProgressiveSerialQueue()
  114. init?(
  115. options: KingfisherParsedOptionsInfo,
  116. refresh: @escaping (KFCrossPlatformImage) -> Void
  117. ) {
  118. guard let progressive = options.progressiveJPEG else { return nil }
  119. self.progressive = progressive
  120. self.refresh = refresh
  121. self.decoder = ImageProgressiveDecoder(
  122. progressive,
  123. processingQueue: options.processingQueue ?? sharedProcessingQueue,
  124. creatingOptions: options.imageCreatingOptions
  125. )
  126. }
  127. func update(data: Data, with callbacks: [SessionDataTask.TaskCallback]) {
  128. guard !data.isEmpty else { return }
  129. queue.add(minimum: progressive.scanInterval) { completion in
  130. @Sendable func decode(_ data: Data) {
  131. self.decoder.decode(data, with: callbacks) { image in
  132. defer { completion() }
  133. guard self.onShouldApply() else { return }
  134. guard let image = image else { return }
  135. self.refresh(image)
  136. }
  137. }
  138. Task { @MainActor in
  139. let applyFlag = self.onShouldApply()
  140. guard applyFlag else {
  141. self.queue.clean()
  142. completion()
  143. return
  144. }
  145. if self.progressive.isFastestScan {
  146. decode(self.decoder.scanning(data) ?? Data())
  147. } else {
  148. self.decoder.scanning(data).forEach { decode($0) }
  149. }
  150. }
  151. }
  152. }
  153. }
  154. private final class ImageProgressiveDecoder: @unchecked Sendable {
  155. private let option: ImageProgressive
  156. private let processingQueue: CallbackQueue
  157. private let creatingOptions: ImageCreatingOptions
  158. private(set) var scannedCount = 0
  159. private(set) var scannedIndex = -1
  160. init(_ option: ImageProgressive,
  161. processingQueue: CallbackQueue,
  162. creatingOptions: ImageCreatingOptions) {
  163. self.option = option
  164. self.processingQueue = processingQueue
  165. self.creatingOptions = creatingOptions
  166. }
  167. func scanning(_ data: Data) -> [Data] {
  168. guard data.kf.contains(jpeg: .SOF2) else {
  169. return []
  170. }
  171. guard scannedIndex + 1 < data.count else {
  172. return []
  173. }
  174. var datas: [Data] = []
  175. var index = scannedIndex + 1
  176. var count = scannedCount
  177. while index < data.count - 1 {
  178. scannedIndex = index
  179. // 0xFF, 0xDA - Start Of Scan
  180. let SOS = ImageFormat.JPEGMarker.SOS.bytes
  181. if data[index] == SOS[0], data[index + 1] == SOS[1] {
  182. if count > 0 {
  183. datas.append(data[0 ..< index])
  184. }
  185. count += 1
  186. }
  187. index += 1
  188. }
  189. // Found more scans this the previous time
  190. guard count > scannedCount else { return [] }
  191. scannedCount = count
  192. // `> 1` checks that we've received a first scan (SOS) and then received
  193. // and also received a second scan (SOS). This way we know that we have
  194. // at least one full scan available.
  195. guard count > 1 else { return [] }
  196. return datas
  197. }
  198. func scanning(_ data: Data) -> Data? {
  199. guard data.kf.contains(jpeg: .SOF2) else {
  200. return nil
  201. }
  202. guard scannedIndex + 1 < data.count else {
  203. return nil
  204. }
  205. var index = scannedIndex + 1
  206. var count = scannedCount
  207. var lastSOSIndex = 0
  208. while index < data.count - 1 {
  209. scannedIndex = index
  210. // 0xFF, 0xDA - Start Of Scan
  211. let SOS = ImageFormat.JPEGMarker.SOS.bytes
  212. if data[index] == SOS[0], data[index + 1] == SOS[1] {
  213. lastSOSIndex = index
  214. count += 1
  215. }
  216. index += 1
  217. }
  218. // Found more scans this the previous time
  219. guard count > scannedCount else { return nil }
  220. scannedCount = count
  221. // `> 1` checks that we've received a first scan (SOS) and then received
  222. // and also received a second scan (SOS). This way we know that we have
  223. // at least one full scan available.
  224. guard count > 1 && lastSOSIndex > 0 else { return nil }
  225. return data[0 ..< lastSOSIndex]
  226. }
  227. func decode(_ data: Data,
  228. with callbacks: [SessionDataTask.TaskCallback],
  229. completion: @escaping @Sendable (KFCrossPlatformImage?) -> Void) {
  230. guard data.kf.contains(jpeg: .SOF2) else {
  231. CallbackQueue.mainCurrentOrAsync.execute { completion(nil) }
  232. return
  233. }
  234. @Sendable func processing(_ data: Data) {
  235. let processor = ImageDataProcessor(
  236. data: data,
  237. callbacks: callbacks,
  238. processingQueue: processingQueue
  239. )
  240. processor.onImageProcessed.delegate(on: self) { (self, result) in
  241. guard let image = try? result.0.get() else {
  242. CallbackQueue.mainCurrentOrAsync.execute { completion(nil) }
  243. return
  244. }
  245. CallbackQueue.mainCurrentOrAsync.execute { completion(image) }
  246. }
  247. processor.process()
  248. }
  249. // Blur partial images.
  250. let count = scannedCount
  251. if option.isBlur, count < 6 {
  252. processingQueue.execute {
  253. // Progressively reduce blur as we load more scans.
  254. let image = KingfisherWrapper<KFCrossPlatformImage>.image(
  255. data: data,
  256. options: self.creatingOptions
  257. )
  258. let radius = max(2, 14 - count * 4)
  259. let temp = image?.kf.blurred(withRadius: CGFloat(radius))
  260. processing(temp?.kf.data(format: .JPEG) ?? data)
  261. }
  262. } else {
  263. processing(data)
  264. }
  265. }
  266. }
  267. private final class ImageProgressiveSerialQueue: @unchecked Sendable {
  268. typealias ClosureCallback = @Sendable ((@escaping @Sendable () -> Void)) -> Void
  269. private let queue: DispatchQueue
  270. private var items: [DispatchWorkItem] = []
  271. private var notify: (() -> Void)?
  272. private var lastTime: TimeInterval?
  273. init() {
  274. self.queue = DispatchQueue(label: "com.onevcat.Kingfisher.ImageProgressive.SerialQueue")
  275. }
  276. func add(minimum interval: TimeInterval, closure: @escaping ClosureCallback) {
  277. let completion = { @Sendable [weak self] in
  278. guard let self = self else { return }
  279. self.queue.async { [weak self] in
  280. guard let self = self else { return }
  281. guard !self.items.isEmpty else { return }
  282. self.items.removeFirst()
  283. if let next = self.items.first {
  284. self.queue.asyncAfter(
  285. deadline: .now() + interval,
  286. execute: next
  287. )
  288. } else {
  289. self.lastTime = Date().timeIntervalSince1970
  290. self.notify?()
  291. self.notify = nil
  292. }
  293. }
  294. }
  295. queue.async { [weak self] in
  296. guard let self = self else { return }
  297. let item = DispatchWorkItem {
  298. closure(completion)
  299. }
  300. if self.items.isEmpty {
  301. let difference = Date().timeIntervalSince1970 - (self.lastTime ?? 0)
  302. let delay = difference < interval ? interval - difference : 0
  303. self.queue.asyncAfter(deadline: .now() + delay, execute: item)
  304. }
  305. self.items.append(item)
  306. }
  307. }
  308. func clean() {
  309. queue.async { [weak self] in
  310. guard let self = self else { return }
  311. self.items.forEach { $0.cancel() }
  312. self.items.removeAll()
  313. }
  314. }
  315. }