PHPickerResultImageDataProvider.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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)
  28. import PhotosUI
  29. /// A data provider to provide image data from a given `PHPickerResult`.
  30. @available(iOS 14.0, macOS 13.0, *)
  31. public struct PHPickerResultImageDataProvider: ImageDataProvider {
  32. /// The possible error might be caused by the `PHPickerResultImageDataProvider`.
  33. /// - invalidImage: The retrieved image is invalid.
  34. public enum PHPickerResultImageDataProviderError: Error {
  35. /// The retrieved image is invalid.
  36. case invalidImage
  37. }
  38. /// The picker result bound to `self`.
  39. public let pickerResult: PHPickerResult
  40. /// The content type of the image.
  41. public let contentType: UTType
  42. private var internalKey: String {
  43. pickerResult.assetIdentifier ?? UUID().uuidString
  44. }
  45. public var cacheKey: String {
  46. "\(internalKey)_\(contentType.identifier)"
  47. }
  48. /// Creates an image data provider from a given `PHPickerResult`.
  49. /// - Parameters:
  50. /// - pickerResult: The picker result to provide image data.
  51. /// - contentType: The content type of the image. Default is `UTType.image`.
  52. public init(pickerResult: PHPickerResult, contentType: UTType = UTType.image) {
  53. self.pickerResult = pickerResult
  54. self.contentType = contentType
  55. }
  56. public func data(handler: @escaping (Result<Data, Error>) -> Void) {
  57. pickerResult.itemProvider.loadDataRepresentation(forTypeIdentifier: contentType.identifier) { data, error in
  58. if let error {
  59. handler(.failure(error))
  60. return
  61. }
  62. guard let data else {
  63. handler(.failure(PHPickerResultImageDataProviderError.invalidImage))
  64. return
  65. }
  66. handler(.success(data))
  67. }
  68. }
  69. }
  70. #endif