ImageDownloader+LivePhoto.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 LivePhotoResourceLoadingResult: 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. /// - image: The image of the download result.
  40. /// - url: The URL from which the image was downloaded.
  41. /// - originalData: The binary data of the image.
  42. public init(originalData: Data, url: URL? = nil) {
  43. self.url = url
  44. self.originalData = originalData
  45. }
  46. }
  47. extension ImageDownloader {
  48. public func downloadLivePhotoResource(
  49. with url: URL,
  50. options: KingfisherParsedOptionsInfo
  51. ) async throws -> LivePhotoResourceLoadingResult {
  52. let task = CancellationDownloadTask()
  53. return try await withTaskCancellationHandler {
  54. try await withCheckedThrowingContinuation { continuation in
  55. let downloadTask = downloadLivePhotoResource(with: url, options: options) { result in
  56. continuation.resume(with: result)
  57. }
  58. if Task.isCancelled {
  59. downloadTask.cancel()
  60. } else {
  61. Task {
  62. await task.setTask(downloadTask)
  63. }
  64. }
  65. }
  66. } onCancel: {
  67. Task {
  68. await task.task?.cancel()
  69. }
  70. }
  71. }
  72. @discardableResult
  73. public func downloadLivePhotoResource(
  74. with url: URL,
  75. options: KingfisherParsedOptionsInfo,
  76. completionHandler: (@Sendable (Result<LivePhotoResourceLoadingResult, KingfisherError>) -> Void)? = nil
  77. ) -> DownloadTask {
  78. var checkedOptions = options
  79. if options.processor != LivePhotoImageProcessor.default {
  80. assertionFailure("[Kingfisher] Using of custom processors during loading of live photo resource is not supported.")
  81. checkedOptions.processor = LivePhotoImageProcessor.default
  82. }
  83. return downloadImage(with: url, options: checkedOptions) { result in
  84. guard let completionHandler else {
  85. return
  86. }
  87. let newResult = result.map { LivePhotoResourceLoadingResult(originalData: $0.originalData, url: $0.url) }
  88. completionHandler(newResult)
  89. }
  90. }
  91. }