Image+WebP.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // Image+WebP.swift
  3. // Pods
  4. //
  5. // Created by yeatse on 2016/10/19.
  6. //
  7. //
  8. import Kingfisher
  9. import CoreGraphics
  10. import Foundation
  11. #if SWIFT_PACKAGE
  12. import KingfisherWebP_ObjC
  13. #endif
  14. // MARK: - Image Representation
  15. extension KingfisherWrapper where Base: KFCrossPlatformImage {
  16. /// isLossy (0=lossy , 1=lossless (default)).
  17. /// Note that the default values are isLossy= false and quality=75.0f
  18. public func webpRepresentation(isLossy: Bool = false, quality: Float = 75.0) -> Data? {
  19. if let result = animatedWebPRepresentation(isLossy: isLossy, quality: quality) {
  20. return result
  21. }
  22. #if os(macOS)
  23. if let cgImage = base.cgImage(forProposedRect: nil, context: nil, hints: nil) {
  24. return WebPDataCreateWithImage(cgImage, isLossy, quality) as Data?
  25. }
  26. #else
  27. if let cgImage = base.cgImage {
  28. return WebPDataCreateWithImage(cgImage, isLossy, quality) as Data?
  29. }
  30. #endif
  31. return nil
  32. }
  33. /// isLossy (0=lossy , 1=lossless (default)).
  34. /// Note that the default values are isLossy= false and quality=75.0f
  35. private func animatedWebPRepresentation(isLossy: Bool = false, quality: Float = 75.0) -> Data? {
  36. #if os(macOS)
  37. return nil
  38. #else
  39. guard let images = base.images?.compactMap({ $0.cgImage }) else {
  40. return nil
  41. }
  42. let imageInfo = [ kWebPAnimatedImageFrames: images,
  43. kWebPAnimatedImageDuration: NSNumber(value: base.duration) ] as [CFString : Any]
  44. return WebPDataCreateWithAnimatedImageInfo(imageInfo as CFDictionary, isLossy, quality) as Data?
  45. #endif
  46. }
  47. }
  48. // MARK: - Create image from WebP data
  49. extension KingfisherWrapper where Base: KFCrossPlatformImage {
  50. public static func image(webpData: Data, scale: CGFloat, onlyFirstFrame: Bool) -> KFCrossPlatformImage? {
  51. let frameCount = WebPImageFrameCountGetFromData(webpData as CFData)
  52. if (frameCount == 0) {
  53. return nil
  54. }
  55. #if os(macOS)
  56. guard let cgImage = WebPImageCreateWithData(webpData as CFData) else {
  57. return nil
  58. }
  59. let image = KFCrossPlatformImage(cgImage: cgImage, size: .zero)
  60. image.kf.imageFrameCount = Int(frameCount)
  61. return image
  62. #else
  63. if (frameCount == 1 || onlyFirstFrame) {
  64. guard let cgImage = WebPImageCreateWithData(webpData as CFData) else {
  65. return nil
  66. }
  67. let image = KFCrossPlatformImage(cgImage: cgImage, scale: scale, orientation: .up)
  68. image.kf.imageFrameCount = Int(frameCount)
  69. return image
  70. }
  71. // MARK: Animated images
  72. guard let animationInfo = WebPAnimatedImageInfoCreateWithData(webpData as CFData) as Dictionary? else {
  73. return nil
  74. }
  75. guard let cgFrames = animationInfo[kWebPAnimatedImageFrames] as? [CGImage] else {
  76. return nil
  77. }
  78. let uiFrames = cgFrames.map { KFCrossPlatformImage(cgImage: $0, scale: scale, orientation: .up) }
  79. let duration = (animationInfo[kWebPAnimatedImageDuration] as? NSNumber).flatMap { $0.doubleValue as TimeInterval } ?? 0.1 * TimeInterval(frameCount)
  80. let image = KFCrossPlatformImage.animatedImage(with: uiFrames, duration: duration)
  81. image?.kf.imageFrameCount = Int(frameCount)
  82. return image
  83. #endif
  84. }
  85. }
  86. // MARK: - WebP Format Testing
  87. extension Data {
  88. public var isWebPFormat: Bool {
  89. if count < 12 {
  90. return false
  91. }
  92. let endIndex = index(startIndex, offsetBy: 12)
  93. let testData = subdata(in: startIndex..<endIndex)
  94. guard let testString = String(data: testData, encoding: .ascii) else {
  95. return false
  96. }
  97. if testString.hasPrefix("RIFF") && testString.hasSuffix("WEBP") {
  98. return true
  99. } else {
  100. return false
  101. }
  102. }
  103. }