You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
3.3 KiB

  1. //
  2. // ImageDataProcessor.swift
  3. // Kingfisher
  4. //
  5. // Created by Wei Wang on 2018/10/11.
  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. private let sharedProcessingQueue: CallbackQueue =
  28. .dispatch(DispatchQueue(label: "com.onevcat.Kingfisher.ImageDownloader.Process"))
  29. // Handles image processing work on an own process queue.
  30. class ImageDataProcessor {
  31. let data: Data
  32. let callbacks: [SessionDataTask.TaskCallback]
  33. let queue: CallbackQueue
  34. // Note: We have an optimization choice there, to reduce queue dispatch by checking callback
  35. // queue settings in each option...
  36. let onImageProcessed = Delegate<(Result<KFCrossPlatformImage, KingfisherError>, SessionDataTask.TaskCallback), Void>()
  37. init(data: Data, callbacks: [SessionDataTask.TaskCallback], processingQueue: CallbackQueue?) {
  38. self.data = data
  39. self.callbacks = callbacks
  40. self.queue = processingQueue ?? sharedProcessingQueue
  41. }
  42. func process() {
  43. queue.execute(doProcess)
  44. }
  45. private func doProcess() {
  46. var processedImages = [String: KFCrossPlatformImage]()
  47. for callback in callbacks {
  48. let processor = callback.options.processor
  49. var image = processedImages[processor.identifier]
  50. if image == nil {
  51. image = processor.process(item: .data(data), options: callback.options)
  52. processedImages[processor.identifier] = image
  53. }
  54. let result: Result<KFCrossPlatformImage, KingfisherError>
  55. if let image = image {
  56. var finalImage = image
  57. if let imageModifier = callback.options.imageModifier {
  58. finalImage = imageModifier.modify(image)
  59. }
  60. if callback.options.backgroundDecode {
  61. finalImage = finalImage.kf.decoded
  62. }
  63. result = .success(finalImage)
  64. } else {
  65. let error = KingfisherError.processorError(
  66. reason: .processingFailed(processor: processor, item: .data(data)))
  67. result = .failure(error)
  68. }
  69. onImageProcessed.call((result, callback))
  70. }
  71. }
  72. }