You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
5.3 KiB

  1. //
  2. // AnimatedImage.swift
  3. // Kingfisher
  4. //
  5. // Created by onevcat on 2018/09/26.
  6. //
  7. // Copyright (c) 2019 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 ImageIO
  28. /// Represents a set of image creating options used in Kingfisher.
  29. public struct ImageCreatingOptions {
  30. /// The target scale of image needs to be created.
  31. public let scale: CGFloat
  32. /// The expected animation duration if an animated image being created.
  33. public let duration: TimeInterval
  34. /// For an animated image, whether or not all frames should be loaded before displaying.
  35. public let preloadAll: Bool
  36. /// For an animated image, whether or not only the first image should be
  37. /// loaded as a static image. It is useful for preview purpose of an animated image.
  38. public let onlyFirstFrame: Bool
  39. /// Creates an `ImageCreatingOptions` object.
  40. ///
  41. /// - Parameters:
  42. /// - scale: The target scale of image needs to be created. Default is `1.0`.
  43. /// - duration: The expected animation duration if an animated image being created.
  44. /// A value less or equal to `0.0` means the animated image duration will
  45. /// be determined by the frame data. Default is `0.0`.
  46. /// - preloadAll: For an animated image, whether or not all frames should be loaded before displaying.
  47. /// Default is `false`.
  48. /// - onlyFirstFrame: For an animated image, whether or not only the first image should be
  49. /// loaded as a static image. It is useful for preview purpose of an animated image.
  50. /// Default is `false`.
  51. public init(
  52. scale: CGFloat = 1.0,
  53. duration: TimeInterval = 0.0,
  54. preloadAll: Bool = false,
  55. onlyFirstFrame: Bool = false)
  56. {
  57. self.scale = scale
  58. self.duration = duration
  59. self.preloadAll = preloadAll
  60. self.onlyFirstFrame = onlyFirstFrame
  61. }
  62. }
  63. // Represents the decoding for a GIF image. This class extracts frames from an `imageSource`, then
  64. // hold the images for later use.
  65. class GIFAnimatedImage {
  66. let images: [KFCrossPlatformImage]
  67. let duration: TimeInterval
  68. init?(from imageSource: CGImageSource, for info: [String: Any], options: ImageCreatingOptions) {
  69. let frameCount = CGImageSourceGetCount(imageSource)
  70. var images = [KFCrossPlatformImage]()
  71. var gifDuration = 0.0
  72. for i in 0 ..< frameCount {
  73. guard let imageRef = CGImageSourceCreateImageAtIndex(imageSource, i, info as CFDictionary) else {
  74. return nil
  75. }
  76. if frameCount == 1 {
  77. gifDuration = .infinity
  78. } else {
  79. // Get current animated GIF frame duration
  80. gifDuration += GIFAnimatedImage.getFrameDuration(from: imageSource, at: i)
  81. }
  82. images.append(KingfisherWrapper.image(cgImage: imageRef, scale: options.scale, refImage: nil))
  83. if options.onlyFirstFrame { break }
  84. }
  85. self.images = images
  86. self.duration = gifDuration
  87. }
  88. // Calculates frame duration for a gif frame out of the kCGImagePropertyGIFDictionary dictionary.
  89. static func getFrameDuration(from gifInfo: [String: Any]?) -> TimeInterval {
  90. let defaultFrameDuration = 0.1
  91. guard let gifInfo = gifInfo else { return defaultFrameDuration }
  92. let unclampedDelayTime = gifInfo[kCGImagePropertyGIFUnclampedDelayTime as String] as? NSNumber
  93. let delayTime = gifInfo[kCGImagePropertyGIFDelayTime as String] as? NSNumber
  94. let duration = unclampedDelayTime ?? delayTime
  95. guard let frameDuration = duration else { return defaultFrameDuration }
  96. return frameDuration.doubleValue > 0.011 ? frameDuration.doubleValue : defaultFrameDuration
  97. }
  98. // Calculates frame duration at a specific index for a gif from an `imageSource`.
  99. static func getFrameDuration(from imageSource: CGImageSource, at index: Int) -> TimeInterval {
  100. guard let properties = CGImageSourceCopyPropertiesAtIndex(imageSource, index, nil)
  101. as? [String: Any] else { return 0.0 }
  102. let gifInfo = properties[kCGImagePropertyGIFDictionary as String] as? [String: Any]
  103. return getFrameDuration(from: gifInfo)
  104. }
  105. }