2014-09-06 14:12:09 +00:00
|
|
|
import UIKit
|
|
|
|
import ImageIO
|
|
|
|
import MobileCoreServices
|
|
|
|
|
|
|
|
func CGImageSourceContainsAnimatedGIF(imageSource: CGImageSource) -> Bool {
|
|
|
|
let isTypeGIF = UTTypeConformsTo(CGImageSourceGetType(imageSource), kUTTypeGIF)
|
|
|
|
let imageCount = CGImageSourceGetCount(imageSource)
|
|
|
|
return isTypeGIF != 0 && imageCount > 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func CGImageSourceGIFFrameDuration(imageSource: CGImageSource, index: Int) -> NSTimeInterval {
|
|
|
|
let containsAnimatedGIF = CGImageSourceContainsAnimatedGIF(imageSource)
|
|
|
|
if !containsAnimatedGIF { return 0.0 }
|
|
|
|
|
|
|
|
var duration = 0.0
|
|
|
|
let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, UInt(index), nil) as NSDictionary
|
2014-11-07 00:04:34 +00:00
|
|
|
let GIFProperties: NSDictionary? = imageProperties.objectForKey(kCGImagePropertyGIFDictionary) as? NSDictionary
|
2014-09-06 14:12:09 +00:00
|
|
|
|
|
|
|
if let properties = GIFProperties {
|
2014-11-07 00:04:34 +00:00
|
|
|
duration = properties.valueForKey(kCGImagePropertyGIFUnclampedDelayTime) as Double
|
2014-09-06 14:12:09 +00:00
|
|
|
|
|
|
|
if duration <= 0 {
|
2014-11-07 00:04:34 +00:00
|
|
|
duration = properties.valueForKey(kCGImagePropertyGIFDelayTime) as Double
|
2014-09-06 14:12:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let threshold = 0.02 - Double(FLT_EPSILON)
|
|
|
|
|
|
|
|
if duration > 0 && duration < threshold {
|
|
|
|
duration = 0.1
|
|
|
|
}
|
|
|
|
|
|
|
|
return duration
|
|
|
|
}
|