PHPickerResultImageDataProvider.swift 3.0 KB

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