2017-07-19 09:34:33 +00:00
|
|
|
import Foundation
|
2018-02-17 15:14:53 +00:00
|
|
|
import UIKit
|
2017-07-19 09:34:33 +00:00
|
|
|
|
2016-09-26 21:01:27 +00:00
|
|
|
/// The protocol that view classes need to conform to to enable animated GIF support.
|
2016-10-01 11:41:22 +00:00
|
|
|
public protocol GIFAnimatable: class {
|
2016-09-26 21:01:27 +00:00
|
|
|
/// Responsible for managing the animation frames.
|
|
|
|
var animator: Animator? { get set }
|
|
|
|
|
|
|
|
/// Notifies the instance that it needs display.
|
|
|
|
var layer: CALayer { get }
|
|
|
|
|
|
|
|
/// View frame used for resizing the frames.
|
|
|
|
var frame: CGRect { get set }
|
|
|
|
|
|
|
|
/// Content mode used for resizing the frames.
|
|
|
|
var contentMode: UIViewContentMode { get set }
|
|
|
|
}
|
2016-10-01 11:41:22 +00:00
|
|
|
|
2016-10-08 17:36:32 +00:00
|
|
|
|
|
|
|
/// A single-property protocol that animatable classes can optionally conform to.
|
|
|
|
public protocol ImageContainer {
|
|
|
|
/// Used for displaying the animation frames.
|
|
|
|
var image: UIImage? { get set }
|
|
|
|
}
|
|
|
|
|
|
|
|
extension GIFAnimatable where Self: ImageContainer {
|
2016-09-26 21:01:27 +00:00
|
|
|
/// Returns the intrinsic content size based on the size of the image.
|
|
|
|
public var intrinsicContentSize: CGSize {
|
|
|
|
return image?.size ?? CGSize.zero
|
|
|
|
}
|
2016-10-08 17:36:32 +00:00
|
|
|
}
|
2016-09-26 21:01:27 +00:00
|
|
|
|
2016-10-08 17:36:32 +00:00
|
|
|
extension GIFAnimatable {
|
2016-11-13 22:46:53 +00:00
|
|
|
/// Total duration of one animation loop
|
|
|
|
public var gifLoopDuration: TimeInterval {
|
|
|
|
return animator?.loopDuration ?? 0
|
|
|
|
}
|
2017-07-19 09:34:33 +00:00
|
|
|
|
2016-10-06 20:49:31 +00:00
|
|
|
/// Returns the active frame if available.
|
|
|
|
public var activeFrame: UIImage? {
|
|
|
|
return animator?.activeFrame()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Total frame count of the GIF.
|
|
|
|
public var frameCount: Int {
|
|
|
|
return animator?.frameCount ?? 0
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Introspect whether the instance is animating.
|
|
|
|
public var isAnimatingGIF: Bool {
|
|
|
|
return animator?.isAnimating ?? false
|
|
|
|
}
|
|
|
|
|
2016-09-26 21:01:27 +00:00
|
|
|
/// Prepare for animation and start animating immediately.
|
|
|
|
///
|
2016-10-06 20:49:31 +00:00
|
|
|
/// - parameter imageName: The file name of the GIF in the main bundle.
|
2016-11-13 22:46:53 +00:00
|
|
|
/// - parameter loopCount: Desired number of loops, <= 0 for infinite loop.
|
2018-01-01 20:12:33 +00:00
|
|
|
/// - parameter completionHandler: Completion callback function
|
|
|
|
public func animate(withGIFNamed imageName: String, loopCount: Int = 0, completionHandler: (() -> Void)? = nil) {
|
2017-07-19 09:34:33 +00:00
|
|
|
animator?.animate(withGIFNamed: imageName,
|
|
|
|
size: frame.size,
|
|
|
|
contentMode: contentMode,
|
2018-01-01 20:12:33 +00:00
|
|
|
loopCount: loopCount,
|
|
|
|
completionHandler: completionHandler)
|
2016-09-26 21:01:27 +00:00
|
|
|
}
|
|
|
|
|
2016-10-06 20:49:31 +00:00
|
|
|
/// Prepare for animation and start animating immediately.
|
|
|
|
///
|
|
|
|
/// - parameter imageData: GIF image data.
|
2016-11-13 22:46:53 +00:00
|
|
|
/// - parameter loopCount: Desired number of loops, <= 0 for infinite loop.
|
2018-01-01 20:12:33 +00:00
|
|
|
/// - parameter completionHandler: Completion callback function
|
|
|
|
public func animate(withGIFData imageData: Data, loopCount: Int = 0, completionHandler: (() -> Void)? = nil) {
|
2017-07-19 09:34:33 +00:00
|
|
|
animator?.animate(withGIFData: imageData,
|
|
|
|
size: frame.size,
|
|
|
|
contentMode: contentMode,
|
2018-01-01 20:12:33 +00:00
|
|
|
loopCount: loopCount,
|
|
|
|
completionHandler: completionHandler)
|
2017-07-19 09:34:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Prepare for animation and start animating immediately.
|
|
|
|
///
|
|
|
|
/// - parameter imageURL: GIF image url.
|
|
|
|
/// - parameter loopCount: Desired number of loops, <= 0 for infinite loop.
|
2018-01-01 20:12:33 +00:00
|
|
|
/// - parameter completionHandler: Completion callback function
|
|
|
|
public func animate(withGIFURL imageURL: URL, loopCount: Int = 0, completionHandler: (() -> Void)? = nil) {
|
2017-07-19 09:34:33 +00:00
|
|
|
let session = URLSession.shared
|
|
|
|
|
|
|
|
let task = session.dataTask(with: imageURL) { (data, response, error) in
|
|
|
|
switch (data, response, error) {
|
|
|
|
case (.none, _, let error?):
|
|
|
|
print("Error downloading gif:", error.localizedDescription, "at url:", imageURL.absoluteString)
|
|
|
|
case (let data?, _, _):
|
|
|
|
DispatchQueue.main.async {
|
2018-01-01 20:12:33 +00:00
|
|
|
self.animate(withGIFData: data, loopCount: loopCount, completionHandler: completionHandler)
|
2017-07-19 09:34:33 +00:00
|
|
|
}
|
|
|
|
default: ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
task.resume()
|
2016-09-26 21:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Prepares the animator instance for animation.
|
|
|
|
///
|
2016-10-01 11:41:22 +00:00
|
|
|
/// - parameter imageName: The file name of the GIF in the main bundle.
|
2016-11-13 22:46:53 +00:00
|
|
|
/// - parameter loopCount: Desired number of loops, <= 0 for infinite loop.
|
2017-07-19 09:34:33 +00:00
|
|
|
public func prepareForAnimation(withGIFNamed imageName: String,
|
|
|
|
loopCount: Int = 0,
|
|
|
|
completionHandler: (() -> Void)? = nil) {
|
|
|
|
animator?.prepareForAnimation(withGIFNamed: imageName,
|
|
|
|
size: frame.size,
|
|
|
|
contentMode: contentMode,
|
|
|
|
loopCount: loopCount,
|
|
|
|
completionHandler: completionHandler)
|
2016-09-26 21:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Prepare for animation and start animating immediately.
|
|
|
|
///
|
2016-10-01 11:41:22 +00:00
|
|
|
/// - parameter imageData: GIF image data.
|
2016-11-13 22:46:53 +00:00
|
|
|
/// - parameter loopCount: Desired number of loops, <= 0 for infinite loop.
|
2017-07-19 09:34:33 +00:00
|
|
|
public func prepareForAnimation(withGIFData imageData: Data,
|
|
|
|
loopCount: Int = 0,
|
|
|
|
completionHandler: (() -> Void)? = nil) {
|
2016-10-08 17:36:32 +00:00
|
|
|
if var imageContainer = self as? ImageContainer {
|
|
|
|
imageContainer.image = UIImage(data: imageData)
|
|
|
|
}
|
|
|
|
|
2017-07-19 09:34:33 +00:00
|
|
|
animator?.prepareForAnimation(withGIFData: imageData,
|
|
|
|
size: frame.size,
|
|
|
|
contentMode: contentMode,
|
|
|
|
loopCount: loopCount,
|
|
|
|
completionHandler: completionHandler)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Prepare for animation and start animating immediately.
|
|
|
|
///
|
|
|
|
/// - parameter imageURL: GIF image url.
|
|
|
|
/// - parameter loopCount: Desired number of loops, <= 0 for infinite loop.
|
|
|
|
public func prepareForAnimation(withGIFURL imageURL: URL,
|
|
|
|
loopCount: Int = 0,
|
|
|
|
completionHandler: (() -> Void)? = nil) {
|
|
|
|
let session = URLSession.shared
|
|
|
|
let task = session.dataTask(with: imageURL) { (data, response, error) in
|
|
|
|
switch (data, response, error) {
|
|
|
|
case (.none, _, let error?):
|
|
|
|
print("Error downloading gif:", error.localizedDescription, "at url:", imageURL.absoluteString)
|
|
|
|
case (let data?, _, _):
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.prepareForAnimation(withGIFData: data,
|
|
|
|
loopCount: loopCount,
|
|
|
|
completionHandler: completionHandler)
|
|
|
|
}
|
|
|
|
default: ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
task.resume()
|
2016-09-26 21:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Stop animating and free up GIF data from memory.
|
|
|
|
public func prepareForReuse() {
|
|
|
|
animator?.prepareForReuse()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Start animating GIF.
|
|
|
|
public func startAnimatingGIF() {
|
|
|
|
animator?.startAnimating()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Stop animating GIF.
|
|
|
|
public func stopAnimatingGIF() {
|
|
|
|
animator?.stopAnimating()
|
|
|
|
}
|
|
|
|
|
2016-10-09 16:12:29 +00:00
|
|
|
/// Whether the frame images should be resized or not. The default is `false`, which means that the frame images retain their original size.
|
|
|
|
///
|
|
|
|
/// - parameter resize: Boolean value indicating whether individual frames should be resized.
|
|
|
|
public func setShouldResizeFrames(_ resize: Bool) {
|
|
|
|
animator?.shouldResizeFrames = resize
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the number of frames that should be buffered. Default is 50. A high number will result in more memory usage and less CPU load, and vice versa.
|
|
|
|
///
|
|
|
|
/// - parameter frames: The number of frames to buffer.
|
|
|
|
public func setFrameBufferCount(_ frames: Int) {
|
|
|
|
animator?.frameBufferCount = frames
|
|
|
|
}
|
|
|
|
|
2016-09-26 21:01:27 +00:00
|
|
|
/// Updates the image with a new frame if necessary.
|
|
|
|
public func updateImageIfNeeded() {
|
2016-10-08 17:36:32 +00:00
|
|
|
if var imageContainer = self as? ImageContainer {
|
2018-04-27 15:01:21 +00:00
|
|
|
let container = imageContainer
|
|
|
|
imageContainer.image = activeFrame ?? container.image
|
2016-10-08 17:36:32 +00:00
|
|
|
} else {
|
|
|
|
layer.contents = activeFrame?.cgImage
|
|
|
|
}
|
2016-10-01 11:41:22 +00:00
|
|
|
}
|
2016-09-26 21:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extension GIFAnimatable {
|
|
|
|
/// Calls setNeedsDisplay on the layer whenever the animator has a new frame. Should *not* be called directly.
|
2016-10-06 20:49:31 +00:00
|
|
|
func animatorHasNewFrame() {
|
2016-09-26 21:01:27 +00:00
|
|
|
layer.setNeedsDisplay()
|
|
|
|
}
|
|
|
|
}
|