PHPickerResultImageDataProvider.swift 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // PHPickerResultImageDataProvider.swift
  3. // Kingfisher
  4. //
  5. // Created by nuomi1 on 2024-04-17.
  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. import Foundation
  27. #if os(iOS) || os(macOS) || os(visionOS)
  28. import PhotosUI
  29. #if compiler(>=6)
  30. @available(iOS 14.0, macOS 13.0, *)
  31. extension PHPickerResult: @unchecked @retroactive Sendable { }
  32. #else
  33. @available(iOS 14.0, macOS 13.0, *)
  34. extension PHPickerResult: @unchecked Sendable { }
  35. #endif
  36. /// A data provider to provide image data from a given `PHPickerResult`.
  37. @available(iOS 14.0, macOS 13.0, *)
  38. public struct PHPickerResultImageDataProvider: ImageDataProvider {
  39. /// The possible error might be caused by the `PHPickerResultImageDataProvider`.
  40. /// - invalidImage: The retrieved image is invalid.
  41. public enum PHPickerResultImageDataProviderError: Error {
  42. /// An error happens during picking up image through the item provider of `PHPickerResult`.
  43. case pickerProviderError(any Error)
  44. /// The retrieved image is invalid.
  45. case invalidImage
  46. }
  47. /// The picker result bound to `self`.
  48. public let pickerResult: PHPickerResult
  49. /// The content type of the image.
  50. public let contentType: UTType
  51. private var internalKey: String {
  52. pickerResult.assetIdentifier ?? UUID().uuidString
  53. }
  54. public var cacheKey: String {
  55. "\(internalKey)_\(contentType.identifier)"
  56. }
  57. /// Creates an image data provider from a given `PHPickerResult`.
  58. /// - Parameters:
  59. /// - pickerResult: The picker result to provide image data.
  60. /// - contentType: The content type of the image. Default is `UTType.image`.
  61. public init(pickerResult: PHPickerResult, contentType: UTType = UTType.image) {
  62. self.pickerResult = pickerResult
  63. self.contentType = contentType
  64. }
  65. public func data(handler: @escaping @Sendable (Result<Data, any Error>) -> Void) {
  66. pickerResult.itemProvider.loadDataRepresentation(forTypeIdentifier: contentType.identifier) { data, error in
  67. if let error {
  68. handler(.failure(PHPickerResultImageDataProviderError.pickerProviderError(error)))
  69. return
  70. }
  71. guard let data else {
  72. handler(.failure(PHPickerResultImageDataProviderError.invalidImage))
  73. return
  74. }
  75. handler(.success(data))
  76. }
  77. }
  78. }
  79. #endif