PHPickerResultImageDataProvider.swift 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. @preconcurrency 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. /// An error happens during picking up image through the item provider of `PHPickerResult`.
  36. case pickerProviderError(Error)
  37. /// The retrieved image is invalid.
  38. case invalidImage
  39. }
  40. /// The picker result bound to `self`.
  41. public let pickerResult: PHPickerResult
  42. /// The content type of the image.
  43. public let contentType: UTType
  44. private var internalKey: String {
  45. pickerResult.assetIdentifier ?? UUID().uuidString
  46. }
  47. public var cacheKey: String {
  48. "\(internalKey)_\(contentType.identifier)"
  49. }
  50. /// Creates an image data provider from a given `PHPickerResult`.
  51. /// - Parameters:
  52. /// - pickerResult: The picker result to provide image data.
  53. /// - contentType: The content type of the image. Default is `UTType.image`.
  54. public init(pickerResult: PHPickerResult, contentType: UTType = UTType.image) {
  55. self.pickerResult = pickerResult
  56. self.contentType = contentType
  57. }
  58. public func data(handler: @escaping @Sendable (Result<Data, Error>) -> Void) {
  59. pickerResult.itemProvider.loadDataRepresentation(forTypeIdentifier: contentType.identifier) { data, error in
  60. if let error {
  61. handler(.failure(PHPickerResultImageDataProviderError.pickerProviderError(error)))
  62. return
  63. }
  64. guard let data else {
  65. handler(.failure(PHPickerResultImageDataProviderError.invalidImage))
  66. return
  67. }
  68. handler(.success(data))
  69. }
  70. }
  71. }
  72. #endif