ImageProgressive.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. private let sharedProcessingQueue: CallbackQueue =
  29. .dispatch(DispatchQueue(label: "com.onevcat.Kingfisher.ImageDownloader.Process"))
  30. public struct ImageProgressive {
  31. /// A default `ImageProgressive` could be used across.
  32. public static let `default` = ImageProgressive(
  33. isBlur: true,
  34. isFastestScan: true,
  35. scanInterval: 0
  36. )
  37. /// Whether to enable blur effect processing
  38. let isBlur: Bool
  39. /// Whether to enable the fastest scan
  40. let isFastestScan: Bool
  41. /// Minimum time interval for each scan
  42. let scanInterval: TimeInterval
  43. public init(isBlur: Bool,
  44. isFastestScan: Bool,
  45. scanInterval: TimeInterval) {
  46. self.isBlur = isBlur
  47. self.isFastestScan = isFastestScan
  48. self.scanInterval = scanInterval
  49. }
  50. }
  51. protocol ImageSettable: AnyObject {
  52. var image: KFCrossPlatformImage? { get set }
  53. }
  54. final class ImageProgressiveProvider: DataReceivingSideEffect {
  55. var onShouldApply: () -> Bool = { return true }
  56. func onDataReceived(_ session: URLSession, task: SessionDataTask, data: Data) {
  57. DispatchQueue.main.async {
  58. guard self.onShouldApply() else { return }
  59. self.update(data: task.mutableData, with: task.callbacks)
  60. }
  61. }
  62. private let option: ImageProgressive
  63. private let refresh: (KFCrossPlatformImage) -> Void
  64. private let decoder: ImageProgressiveDecoder
  65. private let queue = ImageProgressiveSerialQueue()
  66. init?(_ options: KingfisherParsedOptionsInfo,
  67. refresh: @escaping (KFCrossPlatformImage) -> Void) {
  68. guard let option = options.progressiveJPEG else { return nil }
  69. self.option = option
  70. self.refresh = refresh
  71. self.decoder = ImageProgressiveDecoder(
  72. option,
  73. processingQueue: options.processingQueue ?? sharedProcessingQueue,
  74. creatingOptions: options.imageCreatingOptions
  75. )
  76. }
  77. func update(data: Data, with callbacks: [SessionDataTask.TaskCallback]) {
  78. guard !data.isEmpty else { return }
  79. queue.add(minimum: option.scanInterval) { completion in
  80. func decode(_ data: Data) {
  81. self.decoder.decode(data, with: callbacks) { image in
  82. defer { completion() }
  83. guard self.onShouldApply() else { return }
  84. guard let image = image else { return }
  85. self.refresh(image)
  86. }
  87. }
  88. CallbackQueue.mainCurrentOrAsync.execute {
  89. guard self.onShouldApply() else {
  90. self.queue.clean()
  91. completion()
  92. return
  93. }
  94. if self.option.isFastestScan {
  95. decode(self.decoder.scanning(data) ?? Data())
  96. } else {
  97. self.decoder.scanning(data).forEach { decode($0) }
  98. }
  99. }
  100. }
  101. }
  102. }
  103. private final class ImageProgressiveDecoder {
  104. private let option: ImageProgressive
  105. private let processingQueue: CallbackQueue
  106. private let creatingOptions: ImageCreatingOptions
  107. private(set) var scannedCount = 0
  108. private(set) var scannedIndex = -1
  109. init(_ option: ImageProgressive,
  110. processingQueue: CallbackQueue,
  111. creatingOptions: ImageCreatingOptions) {
  112. self.option = option
  113. self.processingQueue = processingQueue
  114. self.creatingOptions = creatingOptions
  115. }
  116. func scanning(_ data: Data) -> [Data] {
  117. guard data.kf.contains(jpeg: .SOF2) else {
  118. return []
  119. }
  120. guard scannedIndex + 1 < data.count else {
  121. return []
  122. }
  123. var datas: [Data] = []
  124. var index = scannedIndex + 1
  125. var count = scannedCount
  126. while index < data.count - 1 {
  127. scannedIndex = index
  128. // 0xFF, 0xDA - Start Of Scan
  129. let SOS = ImageFormat.JPEGMarker.SOS.bytes
  130. if data[index] == SOS[0], data[index + 1] == SOS[1] {
  131. if count > 0 {
  132. datas.append(data[0 ..< index])
  133. }
  134. count += 1
  135. }
  136. index += 1
  137. }
  138. // Found more scans this the previous time
  139. guard count > scannedCount else { return [] }
  140. scannedCount = count
  141. // `> 1` checks that we've received a first scan (SOS) and then received
  142. // and also received a second scan (SOS). This way we know that we have
  143. // at least one full scan available.
  144. guard count > 1 else { return [] }
  145. return datas
  146. }
  147. func scanning(_ data: Data) -> Data? {
  148. guard data.kf.contains(jpeg: .SOF2) else {
  149. return nil
  150. }
  151. guard scannedIndex + 1 < data.count else {
  152. return nil
  153. }
  154. var index = scannedIndex + 1
  155. var count = scannedCount
  156. var lastSOSIndex = 0
  157. while index < data.count - 1 {
  158. scannedIndex = index
  159. // 0xFF, 0xDA - Start Of Scan
  160. let SOS = ImageFormat.JPEGMarker.SOS.bytes
  161. if data[index] == SOS[0], data[index + 1] == SOS[1] {
  162. lastSOSIndex = index
  163. count += 1
  164. }
  165. index += 1
  166. }
  167. // Found more scans this the previous time
  168. guard count > scannedCount else { return nil }
  169. scannedCount = count
  170. // `> 1` checks that we've received a first scan (SOS) and then received
  171. // and also received a second scan (SOS). This way we know that we have
  172. // at least one full scan available.
  173. guard count > 1 && lastSOSIndex > 0 else { return nil }
  174. return data[0 ..< lastSOSIndex]
  175. }
  176. func decode(_ data: Data,
  177. with callbacks: [SessionDataTask.TaskCallback],
  178. completion: @escaping (KFCrossPlatformImage?) -> Void) {
  179. guard data.kf.contains(jpeg: .SOF2) else {
  180. CallbackQueue.mainCurrentOrAsync.execute { completion(nil) }
  181. return
  182. }
  183. func processing(_ data: Data) {
  184. let processor = ImageDataProcessor(
  185. data: data,
  186. callbacks: callbacks,
  187. processingQueue: processingQueue
  188. )
  189. processor.onImageProcessed.delegate(on: self) { (self, result) in
  190. guard let image = try? result.0.get() else {
  191. CallbackQueue.mainCurrentOrAsync.execute { completion(nil) }
  192. return
  193. }
  194. CallbackQueue.mainCurrentOrAsync.execute { completion(image) }
  195. }
  196. processor.process()
  197. }
  198. // Blur partial images.
  199. let count = scannedCount
  200. if option.isBlur, count < 6 {
  201. processingQueue.execute {
  202. // Progressively reduce blur as we load more scans.
  203. let image = KingfisherWrapper<KFCrossPlatformImage>.image(
  204. data: data,
  205. options: self.creatingOptions
  206. )
  207. let radius = max(2, 14 - count * 4)
  208. let temp = image?.kf.blurred(withRadius: CGFloat(radius))
  209. processing(temp?.kf.data(format: .JPEG) ?? data)
  210. }
  211. } else {
  212. processing(data)
  213. }
  214. }
  215. }
  216. private final class ImageProgressiveSerialQueue {
  217. typealias ClosureCallback = ((@escaping () -> Void)) -> Void
  218. private let queue: DispatchQueue = //DispatchQueue.main
  219. .init(label: "com.onevcat.Kingfisher.ImageProgressive.SerialQueue")
  220. private var items: [DispatchWorkItem] = []
  221. private var notify: (() -> Void)?
  222. private var lastTime: TimeInterval?
  223. var count: Int { return items.count }
  224. func add(minimum interval: TimeInterval, closure: @escaping ClosureCallback) {
  225. let completion = { [weak self] in
  226. guard let self = self else { return }
  227. self.queue.async { [weak self] in
  228. guard let self = self else { return }
  229. guard !self.items.isEmpty else { return }
  230. self.items.removeFirst()
  231. if let next = self.items.first {
  232. self.queue.asyncAfter(
  233. deadline: .now() + interval,
  234. execute: next
  235. )
  236. } else {
  237. self.lastTime = Date().timeIntervalSince1970
  238. self.notify?()
  239. self.notify = nil
  240. }
  241. }
  242. }
  243. queue.async { [weak self] in
  244. guard let self = self else { return }
  245. let item = DispatchWorkItem {
  246. closure(completion)
  247. }
  248. if self.items.isEmpty {
  249. let difference = Date().timeIntervalSince1970 - (self.lastTime ?? 0)
  250. let delay = difference < interval ? interval - difference : 0
  251. self.queue.asyncAfter(deadline: .now() + delay, execute: item)
  252. }
  253. self.items.append(item)
  254. }
  255. }
  256. func notify(_ closure: @escaping () -> Void) {
  257. self.notify = closure
  258. }
  259. func clean() {
  260. queue.async { [weak self] in
  261. guard let self = self else { return }
  262. self.items.forEach { $0.cancel() }
  263. self.items.removeAll()
  264. }
  265. }
  266. }