Refactor image source helpers
This commit is contained in:
parent
8013faea87
commit
2951bb60a1
|
@ -2,33 +2,53 @@ import UIKit
|
||||||
import ImageIO
|
import ImageIO
|
||||||
import MobileCoreServices
|
import MobileCoreServices
|
||||||
|
|
||||||
private func CGImageSourceContainsAnimatedGIF(imageSource: CGImageSource) -> Bool {
|
internal typealias GIFProperties = [String : Double]
|
||||||
let isTypeGIF = UTTypeConformsTo(CGImageSourceGetType(imageSource), kUTTypeGIF)
|
private let defaultDuration: Double = 0
|
||||||
let imageCount = CGImageSourceGetCount(imageSource)
|
|
||||||
|
func CGImageSourceGIFFrameDuration(imageSource: CGImageSource, index: Int) -> NSTimeInterval {
|
||||||
|
if !imageSource.isAnimatedGIF { return 0.0 }
|
||||||
|
|
||||||
|
let duration = imageSource.GIFPropertiesAtIndex(UInt(index))
|
||||||
|
>>- durationFromGIFProperties
|
||||||
|
>>- capDuration
|
||||||
|
|
||||||
|
return duration ?? defaultDuration
|
||||||
|
}
|
||||||
|
|
||||||
|
private func capDuration(duration: Double) -> Double? {
|
||||||
|
if duration < 0 { return .None }
|
||||||
|
let threshold = 0.02 - Double(FLT_EPSILON)
|
||||||
|
let cappedDuration = duration < threshold ? 0.1 : duration
|
||||||
|
return cappedDuration
|
||||||
|
}
|
||||||
|
|
||||||
|
private func durationFromGIFProperties(properties: GIFProperties) -> Double? {
|
||||||
|
let unclampedDelayTime = properties[String(kCGImagePropertyGIFUnclampedDelayTime)]
|
||||||
|
let delayTime = properties[String(kCGImagePropertyGIFDelayTime)]
|
||||||
|
|
||||||
|
return duration <^> unclampedDelayTime <*> delayTime
|
||||||
|
}
|
||||||
|
|
||||||
|
private func duration(unclampedDelayTime: Double)(delayTime: Double) -> Double {
|
||||||
|
let delayArray = [unclampedDelayTime, delayTime]
|
||||||
|
return delayArray.filter(isPositive).first ?? defaultDuration
|
||||||
|
}
|
||||||
|
|
||||||
|
private func isPositive(value: Double) -> Bool {
|
||||||
|
return value >= 0
|
||||||
|
}
|
||||||
|
|
||||||
|
extension CGImageSourceRef {
|
||||||
|
var isAnimatedGIF: Bool {
|
||||||
|
let isTypeGIF = UTTypeConformsTo(CGImageSourceGetType(self), kUTTypeGIF)
|
||||||
|
let imageCount = CGImageSourceGetCount(self)
|
||||||
return isTypeGIF != 0 && imageCount > 1
|
return isTypeGIF != 0 && imageCount > 1
|
||||||
}
|
}
|
||||||
|
|
||||||
func CGImageSourceGIFFrameDuration(imageSource: CGImageSource, index: Int) -> NSTimeInterval {
|
func GIFPropertiesAtIndex(index: UInt) -> GIFProperties? {
|
||||||
let containsAnimatedGIF = CGImageSourceContainsAnimatedGIF(imageSource)
|
if !isAnimatedGIF { return .None }
|
||||||
if !containsAnimatedGIF { return 0.0 }
|
|
||||||
|
|
||||||
var duration = 0.0
|
let imageProperties = CGImageSourceCopyPropertiesAtIndex(self, index, nil) as Dictionary
|
||||||
let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, UInt(index), nil) as NSDictionary
|
return imageProperties[String(kCGImagePropertyGIFDictionary)] as? GIFProperties
|
||||||
let GIFProperties: NSDictionary? = imageProperties.objectForKey(kCGImagePropertyGIFDictionary) as? NSDictionary
|
|
||||||
|
|
||||||
if let properties = GIFProperties {
|
|
||||||
duration = properties.valueForKey(kCGImagePropertyGIFUnclampedDelayTime) as Double
|
|
||||||
|
|
||||||
if duration <= 0 {
|
|
||||||
duration = properties.valueForKey(kCGImagePropertyGIFDelayTime) as Double
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let threshold = 0.02 - Double(FLT_EPSILON)
|
|
||||||
|
|
||||||
if duration > 0 && duration < threshold {
|
|
||||||
duration = 0.1
|
|
||||||
}
|
|
||||||
|
|
||||||
return duration
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue