ImageDownloader+LivePhoto.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // ImageDownloader+LivePhoto.swift
  3. // Kingfisher
  4. //
  5. // Created by onevcat on 2024/10/01.
  6. //
  7. // Copyright (c) 2024 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. #if os(macOS)
  27. import AppKit
  28. #else
  29. import UIKit
  30. #endif
  31. public struct LivePhotoResourceDownloadingResult: Sendable {
  32. /// The original URL of the image request.
  33. public let url: URL?
  34. /// The raw data received from the downloader.
  35. public let originalData: Data
  36. /// Creates an `ImageDownloadResult` object.
  37. ///
  38. /// - Parameters:
  39. /// - url: The URL from which the image was downloaded.
  40. /// - originalData: The binary data of the image.
  41. public init(originalData: Data, url: URL? = nil) {
  42. self.url = url
  43. self.originalData = originalData
  44. }
  45. }
  46. extension ImageDownloader {
  47. public func downloadLivePhotoResource(
  48. with url: URL,
  49. options: KingfisherParsedOptionsInfo
  50. ) async throws -> LivePhotoResourceDownloadingResult {
  51. let task = CancellationDownloadTask()
  52. return try await withTaskCancellationHandler {
  53. try await withCheckedThrowingContinuation { continuation in
  54. let downloadTask = downloadLivePhotoResource(with: url, options: options) { result in
  55. continuation.resume(with: result)
  56. }
  57. if Task.isCancelled {
  58. downloadTask.cancel()
  59. } else {
  60. Task {
  61. await task.setTask(downloadTask)
  62. }
  63. }
  64. }
  65. } onCancel: {
  66. Task {
  67. await task.task?.cancel()
  68. }
  69. }
  70. }
  71. @discardableResult
  72. public func downloadLivePhotoResource(
  73. with url: URL,
  74. options: KingfisherParsedOptionsInfo,
  75. completionHandler: (@Sendable (Result<LivePhotoResourceDownloadingResult, KingfisherError>) -> Void)? = nil
  76. ) -> DownloadTask {
  77. var checkedOptions = options
  78. if options.processor == DefaultImageProcessor.default {
  79. // The default processor is a default behavior so we replace it silently.
  80. checkedOptions.processor = LivePhotoImageProcessor.default
  81. } else if options.processor != LivePhotoImageProcessor.default {
  82. assertionFailure("[Kingfisher] Using of custom processors during loading of live photo resource is not supported.")
  83. checkedOptions.processor = LivePhotoImageProcessor.default
  84. }
  85. return downloadImage(with: url, options: checkedOptions) { result in
  86. guard let completionHandler else {
  87. return
  88. }
  89. let newResult = result.map { LivePhotoResourceDownloadingResult(originalData: $0.originalData, url: $0.url) }
  90. completionHandler(newResult)
  91. }
  92. }
  93. }