GIFAnimatedImage.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // AnimatedImage.swift
  3. // Kingfisher
  4. //
  5. // Created by onevcat on 2018/09/26.
  6. //
  7. // Copyright (c) 2018 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 CoreGraphics
  28. import ImageIO
  29. public struct ImageCreatingOptions {
  30. public let scale: CGFloat
  31. public let duration: TimeInterval
  32. public let preloadAll: Bool
  33. public let onlyFirstFrame: Bool
  34. public init(
  35. scale: CGFloat = 1.0,
  36. duration: TimeInterval = 0.0,
  37. preloadAll: Bool = false,
  38. onlyFirstFrame: Bool = false)
  39. {
  40. self.scale = scale
  41. self.duration = duration
  42. self.preloadAll = preloadAll
  43. self.onlyFirstFrame = onlyFirstFrame
  44. }
  45. }
  46. class GIFAnimatedImage {
  47. let images: [Image]
  48. let duration: TimeInterval
  49. init?(from imageSource: CGImageSource, for info: [String: Any], options: ImageCreatingOptions) {
  50. let frameCount = CGImageSourceGetCount(imageSource)
  51. var images = [Image]()
  52. var gifDuration = 0.0
  53. for i in 0 ..< frameCount {
  54. guard let imageRef = CGImageSourceCreateImageAtIndex(imageSource, i, info as CFDictionary) else {
  55. return nil
  56. }
  57. if frameCount == 1 {
  58. gifDuration = .infinity
  59. } else {
  60. // Get current animated GIF frame duration
  61. guard let properties = CGImageSourceCopyPropertiesAtIndex(imageSource, i, nil)
  62. as? [String: Any] else
  63. {
  64. return nil
  65. }
  66. let gifInfo = properties[kCGImagePropertyGIFDictionary as String] as? [String: Any]
  67. gifDuration += GIFAnimatedImage.getFrameDuration(from: gifInfo)
  68. }
  69. images.append(KingfisherClass.image(cgImage: imageRef, scale: options.scale, refImage: nil))
  70. if options.onlyFirstFrame { break }
  71. }
  72. self.images = images
  73. self.duration = gifDuration
  74. }
  75. //Calculates frame duration for a gif frame out of the kCGImagePropertyGIFDictionary dictionary.
  76. static func getFrameDuration(from gifInfo: [String: Any]?) -> TimeInterval {
  77. let defaultFrameDuration = 0.1
  78. guard let gifInfo = gifInfo else { return defaultFrameDuration }
  79. let unclampedDelayTime = gifInfo[kCGImagePropertyGIFUnclampedDelayTime as String] as? NSNumber
  80. let delayTime = gifInfo[kCGImagePropertyGIFDelayTime as String] as? NSNumber
  81. let duration = unclampedDelayTime ?? delayTime
  82. guard let frameDuration = duration else { return defaultFrameDuration }
  83. return frameDuration.doubleValue > 0.011 ? frameDuration.doubleValue : defaultFrameDuration
  84. }
  85. }