2014-12-12 20:29:17 +00:00
|
|
|
import UIKit
|
|
|
|
import ImageIO
|
|
|
|
|
2015-01-23 00:02:08 +00:00
|
|
|
/// Responsible for storing and updating the frames of a `AnimatableImageView` instance via delegation.
|
2015-06-04 23:36:47 +00:00
|
|
|
class Animator {
|
2015-01-23 00:02:08 +00:00
|
|
|
/// Maximum duration to increment the frame timer with.
|
2015-06-09 21:28:11 +00:00
|
|
|
let maxTimeStep = 1.0
|
2015-01-23 00:02:08 +00:00
|
|
|
/// An array of animated frames from a single GIF image.
|
2015-06-09 21:28:11 +00:00
|
|
|
var animatedFrames = [AnimatedFrame]()
|
2015-06-04 23:24:34 +00:00
|
|
|
/// The size to resize all frames to
|
2015-06-09 21:28:11 +00:00
|
|
|
let size: CGSize
|
2015-06-04 23:24:34 +00:00
|
|
|
/// The content mode to use when resizing
|
2015-06-09 21:28:11 +00:00
|
|
|
let contentMode: UIViewContentMode
|
2015-06-04 19:38:52 +00:00
|
|
|
/// Maximum number of frames to load at once
|
2015-06-09 21:28:11 +00:00
|
|
|
let maxFrameCount: Int
|
2015-06-04 19:38:52 +00:00
|
|
|
/// The total number of frames in the GIF.
|
2015-06-09 21:28:11 +00:00
|
|
|
var frameCount = 0
|
2015-06-04 19:38:52 +00:00
|
|
|
/// A reference to the original image source.
|
2015-06-09 21:28:11 +00:00
|
|
|
var imageSource: CGImageSourceRef
|
2015-01-23 00:02:08 +00:00
|
|
|
/// The index of the current GIF frame.
|
2015-06-09 21:28:11 +00:00
|
|
|
var currentFrameIndex = 0
|
2015-06-05 15:01:56 +00:00
|
|
|
/// The index of the current GIF frame from the source.
|
2015-06-09 21:28:11 +00:00
|
|
|
var currentPreloadIndex = 0
|
2015-01-23 00:02:08 +00:00
|
|
|
/// Time elapsed since the last frame change. Used to determine when the frame should be updated.
|
2015-06-09 21:28:11 +00:00
|
|
|
var timeSinceLastFrameChange: NSTimeInterval = 0.0
|
2016-03-13 11:30:27 +00:00
|
|
|
/// Specifies whether GIF frames should be pre-scaled.
|
|
|
|
/// - seealso: `needsPrescaling` in AnimatableImageView.
|
|
|
|
var needsPrescaling = true
|
2014-12-12 20:29:17 +00:00
|
|
|
|
2015-01-23 00:02:08 +00:00
|
|
|
/// The current image frame to show.
|
2014-12-12 20:29:17 +00:00
|
|
|
var currentFrame: UIImage? {
|
|
|
|
return frameAtIndex(currentFrameIndex)
|
|
|
|
}
|
|
|
|
|
2015-06-04 23:24:34 +00:00
|
|
|
/// Is this image animatable?
|
|
|
|
var isAnimatable: Bool {
|
|
|
|
return imageSource.isAnimatedGIF
|
2014-12-12 20:29:17 +00:00
|
|
|
}
|
|
|
|
|
2015-01-23 00:02:08 +00:00
|
|
|
/// Initializes an animator instance from raw GIF image data and an `Animatable` delegate.
|
|
|
|
///
|
2015-06-09 21:28:11 +00:00
|
|
|
/// - parameter data: The raw GIF image data.
|
|
|
|
/// - parameter delegate: An `Animatable` delegate.
|
2015-06-04 23:36:47 +00:00
|
|
|
init(data: NSData, size: CGSize, contentMode: UIViewContentMode, framePreloadCount: Int) {
|
2015-06-04 23:24:34 +00:00
|
|
|
let options = [String(kCGImageSourceShouldCache): kCFBooleanFalse]
|
2015-06-09 21:28:11 +00:00
|
|
|
self.imageSource = CGImageSourceCreateWithData(data, options) ?? CGImageSourceCreateIncremental(options)
|
2015-06-04 23:24:34 +00:00
|
|
|
self.size = size
|
|
|
|
self.contentMode = contentMode
|
2015-06-09 21:28:11 +00:00
|
|
|
self.maxFrameCount = framePreloadCount
|
2015-06-04 19:38:52 +00:00
|
|
|
}
|
|
|
|
|
2015-01-22 10:54:27 +00:00
|
|
|
// MARK: - Frames
|
2015-01-23 00:02:08 +00:00
|
|
|
/// Loads the frames from an image source, resizes them, then caches them in `animatedFrames`.
|
2015-06-04 23:36:47 +00:00
|
|
|
func prepareFrames() {
|
2015-06-09 21:28:11 +00:00
|
|
|
frameCount = Int(CGImageSourceGetCount(imageSource))
|
|
|
|
let framesToProcess = min(frameCount, maxFrameCount)
|
2015-06-04 19:38:52 +00:00
|
|
|
animatedFrames.reserveCapacity(framesToProcess)
|
2016-04-24 10:19:13 +00:00
|
|
|
animatedFrames = (0..<framesToProcess).reduce([]) { $0 + [prepareFrame($1)] }
|
2015-06-05 15:01:56 +00:00
|
|
|
currentPreloadIndex = framesToProcess
|
2015-06-04 19:38:52 +00:00
|
|
|
}
|
2014-12-12 21:49:15 +00:00
|
|
|
|
2015-06-04 19:38:52 +00:00
|
|
|
/// Loads a single frame from an image source, resizes it, then returns an `AnimatedFrame`.
|
|
|
|
///
|
2015-06-09 21:28:11 +00:00
|
|
|
/// - parameter index: The index of the GIF image source to prepare
|
|
|
|
/// - returns: An AnimatedFrame object
|
|
|
|
func prepareFrame(index: Int) -> AnimatedFrame {
|
|
|
|
guard let frameImageRef = CGImageSourceCreateImageAtIndex(imageSource, index, nil) else {
|
|
|
|
return AnimatedFrame.null()
|
|
|
|
}
|
2015-06-04 19:38:52 +00:00
|
|
|
|
2015-06-09 21:28:11 +00:00
|
|
|
let frameDuration = CGImageSourceGIFFrameDuration(imageSource, index: index)
|
2015-06-04 19:38:52 +00:00
|
|
|
let image = UIImage(CGImage: frameImageRef)
|
|
|
|
let scaledImage: UIImage?
|
|
|
|
|
2016-03-13 11:30:27 +00:00
|
|
|
if needsPrescaling == true {
|
|
|
|
switch contentMode {
|
|
|
|
case .ScaleAspectFit: scaledImage = image.resizeAspectFit(size)
|
|
|
|
case .ScaleAspectFill: scaledImage = image.resizeAspectFill(size)
|
|
|
|
default: scaledImage = image.resize(size)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
scaledImage = image
|
2014-12-12 20:29:17 +00:00
|
|
|
}
|
2015-06-04 19:38:52 +00:00
|
|
|
|
|
|
|
return AnimatedFrame(image: scaledImage, duration: frameDuration)
|
2014-12-12 20:29:17 +00:00
|
|
|
}
|
|
|
|
|
2015-01-23 00:02:08 +00:00
|
|
|
/// Returns the frame at a particular index.
|
|
|
|
///
|
2015-06-09 21:28:11 +00:00
|
|
|
/// - parameter index: The index of the frame.
|
|
|
|
/// - returns: An optional image at a given frame.
|
|
|
|
func frameAtIndex(index: Int) -> UIImage? {
|
2015-06-05 15:01:56 +00:00
|
|
|
return animatedFrames[index].image
|
2014-12-12 20:29:17 +00:00
|
|
|
}
|
|
|
|
|
2015-01-23 00:02:08 +00:00
|
|
|
/// Updates the current frame if necessary using the frame timer and the duration of each frame in `animatedFrames`.
|
|
|
|
///
|
2015-06-09 21:28:11 +00:00
|
|
|
/// - returns: An optional image at a given frame.
|
2015-06-04 23:24:34 +00:00
|
|
|
func updateCurrentFrame(duration: CFTimeInterval) -> Bool {
|
|
|
|
timeSinceLastFrameChange += min(maxTimeStep, duration)
|
2015-10-22 18:49:31 +00:00
|
|
|
guard let frameDuration = animatedFrames[safe:currentFrameIndex]?.duration where
|
|
|
|
frameDuration <= timeSinceLastFrameChange else { return false }
|
2014-12-12 20:29:17 +00:00
|
|
|
|
2015-10-22 18:49:31 +00:00
|
|
|
timeSinceLastFrameChange -= frameDuration
|
|
|
|
let lastFrameIndex = currentFrameIndex
|
2016-03-30 22:15:14 +00:00
|
|
|
currentFrameIndex = (currentFrameIndex + 1) % animatedFrames.count
|
2015-10-22 18:49:31 +00:00
|
|
|
|
|
|
|
// Loads the next needed frame for progressive loading
|
|
|
|
if animatedFrames.count < frameCount {
|
|
|
|
animatedFrames[lastFrameIndex] = prepareFrame(currentPreloadIndex)
|
2016-03-30 22:15:14 +00:00
|
|
|
currentPreloadIndex = (currentFrameIndex + 1) % frameCount
|
2014-12-12 20:29:17 +00:00
|
|
|
}
|
2015-10-22 18:49:31 +00:00
|
|
|
|
|
|
|
return true
|
2014-12-12 20:29:17 +00:00
|
|
|
}
|
|
|
|
}
|