AVAssetImageDataProvider.swift 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // AVAssetImageDataProvider.swift
  3. // Kingfisher
  4. //
  5. // Created by onevcat on 2020/08/09.
  6. //
  7. // Copyright (c) 2020 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 AVKit
  28. import MobileCoreServices
  29. public struct AVAssetImageDataProvider: ImageDataProvider {
  30. public enum AVAssetImageDataProviderError: Error {
  31. case userCancelled
  32. case invalidImage(_ image: CGImage?)
  33. }
  34. public let assetImageGenerator: AVAssetImageGenerator
  35. public let time: CMTime
  36. private var internalKey: String {
  37. return (assetImageGenerator.asset as? AVURLAsset)?.url.absoluteString ?? UUID().uuidString
  38. }
  39. public var cacheKey: String {
  40. return "\(internalKey)_\(time.value)"
  41. }
  42. public init(assetImageGenerator: AVAssetImageGenerator, time: CMTime) {
  43. self.assetImageGenerator = assetImageGenerator
  44. self.time = time
  45. }
  46. public init(assetURL: URL, time: CMTime) {
  47. let asset = AVAsset(url: assetURL)
  48. let generator = AVAssetImageGenerator(asset: asset)
  49. self.init(assetImageGenerator: generator, time: time)
  50. }
  51. public func data(handler: @escaping (Result<Data, Error>) -> Void) {
  52. assetImageGenerator.generateCGImagesAsynchronously(forTimes: [NSValue(time: time)]) {
  53. (requestedTime, image, imageTime, result, error) in
  54. if let error = error {
  55. handler(.failure(error))
  56. return
  57. }
  58. if result == .cancelled {
  59. handler(.failure(AVAssetImageDataProviderError.userCancelled))
  60. return
  61. }
  62. guard let cgImage = image, let data = cgImage.jpegData else {
  63. handler(.failure(AVAssetImageDataProviderError.invalidImage(image)))
  64. return
  65. }
  66. handler(.success(data))
  67. }
  68. }
  69. }
  70. extension CGImage {
  71. var jpegData: Data? {
  72. guard let mutableData = CFDataCreateMutable(nil, 0),
  73. let destination = CGImageDestinationCreateWithData(mutableData, kUTTypeJPEG, 1, nil)
  74. else {
  75. return nil
  76. }
  77. CGImageDestinationAddImage(destination, self, nil)
  78. guard CGImageDestinationFinalize(destination) else { return nil }
  79. return mutableData as Data
  80. }
  81. }