2014-09-06 14:12:09 +00:00
|
|
|
import ImageIO
|
|
|
|
import MobileCoreServices
|
2015-01-23 00:02:08 +00:00
|
|
|
import UIKit
|
2014-09-06 14:12:09 +00:00
|
|
|
|
2015-01-23 00:02:08 +00:00
|
|
|
typealias GIFProperties = [String : Double]
|
2015-06-09 21:28:11 +00:00
|
|
|
let defaultDuration: Double = 0
|
2014-09-06 14:12:09 +00:00
|
|
|
|
2015-01-23 00:02:08 +00:00
|
|
|
/// Retruns the duration of a frame at a specific index using an image source (an `CGImageSource` instance).
|
|
|
|
///
|
2015-06-09 21:28:11 +00:00
|
|
|
/// - returns: A frame duration.
|
2014-09-06 14:12:09 +00:00
|
|
|
func CGImageSourceGIFFrameDuration(imageSource: CGImageSource, index: Int) -> NSTimeInterval {
|
2014-12-13 10:35:00 +00:00
|
|
|
if !imageSource.isAnimatedGIF { return 0.0 }
|
2014-09-06 14:12:09 +00:00
|
|
|
|
2016-04-24 10:19:13 +00:00
|
|
|
guard let properties = imageSource.GIFPropertiesAtIndex(index),
|
|
|
|
let duration = durationFromGIFProperties(properties),
|
|
|
|
let cappedDuration = capDuration(duration)
|
|
|
|
else { return defaultDuration }
|
2014-09-06 14:12:09 +00:00
|
|
|
|
2016-04-24 10:19:13 +00:00
|
|
|
return cappedDuration
|
2014-12-13 10:35:00 +00:00
|
|
|
}
|
2014-09-06 14:12:09 +00:00
|
|
|
|
2015-01-23 00:02:08 +00:00
|
|
|
/// Ensures that a duration is never smaller than a threshold value.
|
|
|
|
///
|
2015-06-09 21:28:11 +00:00
|
|
|
/// - returns: A capped frame duration.
|
|
|
|
func capDuration(duration: Double) -> Double? {
|
2014-12-13 10:35:00 +00:00
|
|
|
if duration < 0 { return .None }
|
2014-09-06 14:12:09 +00:00
|
|
|
let threshold = 0.02 - Double(FLT_EPSILON)
|
2014-12-13 10:35:00 +00:00
|
|
|
let cappedDuration = duration < threshold ? 0.1 : duration
|
|
|
|
return cappedDuration
|
|
|
|
}
|
|
|
|
|
2015-01-23 00:02:08 +00:00
|
|
|
/// Returns a frame duration from a `GIFProperties` dictionary.
|
|
|
|
///
|
2015-06-09 21:28:11 +00:00
|
|
|
/// - returns: A frame duration.
|
|
|
|
func durationFromGIFProperties(properties: GIFProperties) -> Double? {
|
2016-03-30 22:15:14 +00:00
|
|
|
guard let unclampedDelayTime = properties[String(kCGImagePropertyGIFUnclampedDelayTime)],
|
|
|
|
let delayTime = properties[String(kCGImagePropertyGIFDelayTime)]
|
|
|
|
else { return .None }
|
2014-12-13 10:35:00 +00:00
|
|
|
|
2016-03-30 22:15:14 +00:00
|
|
|
return duration(unclampedDelayTime, delayTime: delayTime)
|
2014-12-13 10:35:00 +00:00
|
|
|
}
|
|
|
|
|
2015-01-23 00:02:08 +00:00
|
|
|
/// Calculates frame duration based on both clamped and unclamped times.
|
|
|
|
///
|
2015-06-09 21:28:11 +00:00
|
|
|
/// - returns: A frame duration.
|
2016-03-30 22:15:14 +00:00
|
|
|
func duration(unclampedDelayTime: Double, delayTime: Double) -> Double {
|
2014-12-13 10:35:00 +00:00
|
|
|
let delayArray = [unclampedDelayTime, delayTime]
|
|
|
|
return delayArray.filter(isPositive).first ?? defaultDuration
|
|
|
|
}
|
2014-09-06 14:12:09 +00:00
|
|
|
|
2015-01-23 00:02:08 +00:00
|
|
|
/// Checks if a `Double` value is positive.
|
|
|
|
///
|
2015-06-09 21:28:11 +00:00
|
|
|
/// - returns: A boolean value that is `true` if the tested value is positive.
|
|
|
|
func isPositive(value: Double) -> Bool {
|
2014-12-13 10:35:00 +00:00
|
|
|
return value >= 0
|
|
|
|
}
|
|
|
|
|
2015-01-23 00:02:08 +00:00
|
|
|
/// An extension of `CGImageSourceRef` that add GIF introspection and easier property retrieval.
|
2014-12-13 10:35:00 +00:00
|
|
|
extension CGImageSourceRef {
|
2015-01-23 00:02:08 +00:00
|
|
|
/// Returns whether the image source contains an animated GIF.
|
|
|
|
///
|
2015-06-09 21:28:11 +00:00
|
|
|
/// - returns: A boolean value that is `true` if the image source contains animated GIF data.
|
2014-12-13 10:35:00 +00:00
|
|
|
var isAnimatedGIF: Bool {
|
2015-06-09 21:28:11 +00:00
|
|
|
let isTypeGIF = UTTypeConformsTo(CGImageSourceGetType(self) ?? "", kUTTypeGIF)
|
2014-12-13 10:35:00 +00:00
|
|
|
let imageCount = CGImageSourceGetCount(self)
|
2015-06-09 21:28:11 +00:00
|
|
|
return isTypeGIF != false && imageCount > 1
|
2014-09-06 14:12:09 +00:00
|
|
|
}
|
|
|
|
|
2015-01-23 00:02:08 +00:00
|
|
|
/// Returns the GIF properties at a specific index.
|
|
|
|
///
|
2015-06-09 21:28:11 +00:00
|
|
|
/// - parameter index: The index of the GIF properties to retrieve.
|
|
|
|
/// - returns: A dictionary containing the GIF properties at the passed in index.
|
2015-04-08 21:24:45 +00:00
|
|
|
func GIFPropertiesAtIndex(index: Int) -> GIFProperties? {
|
2015-06-09 21:28:11 +00:00
|
|
|
let imageProperties = CGImageSourceCopyPropertiesAtIndex(self, index, nil) as Dictionary?
|
|
|
|
return imageProperties?[String(kCGImagePropertyGIFDictionary)] as? GIFProperties
|
2014-12-13 10:35:00 +00:00
|
|
|
}
|
2014-09-06 14:12:09 +00:00
|
|
|
}
|