-
-
Save skyfe79/f1ca59143419cc6576982b90dc014c1e to your computer and use it in GitHub Desktop.
Add Gif to CALayer for Video Overlay
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func startGifAnimation(with url: URL?, in layer: CALayer?) { | |
let animation: CAKeyframeAnimation? = animationForGif(with: url) | |
if let animation = animation { | |
layer?.add(animation, forKey: "contents") | |
} | |
} | |
func animationForGif(with url: URL?) -> CAKeyframeAnimation? { | |
let animation = CAKeyframeAnimation(keyPath: "contents") | |
var frames = [CGImage]() | |
var delayTimes = [NSNumber]() | |
var totalTime: Float = 0.0 | |
// var gifWidth: Float | |
// var gifHeight: Float | |
let gifSource = CGImageSourceCreateWithURL(url! as CFURL, nil) | |
// get frame count | |
let frameCount = CGImageSourceGetCount(gifSource!) | |
for i in 0..<frameCount { | |
// get each frame | |
let frame = CGImageSourceCreateImageAtIndex(gifSource!, i, nil) | |
if let frame = frame { | |
frames.append(frame) | |
} | |
// get gif info with each frame | |
var dict = CGImageSourceCopyPropertiesAtIndex(gifSource!, i, nil) as? [CFString: AnyObject] | |
// get gif size | |
//gifWidth = (dict?[kCGImagePropertyPixelWidth] as? NSNumber)?.floatValue ?? 0.0 | |
//gifHeight = (dict?[kCGImagePropertyPixelHeight] as? NSNumber)?.floatValue ?? 0.0 | |
let gifDict = dict?[kCGImagePropertyGIFDictionary] | |
if let value = gifDict?[kCGImagePropertyGIFDelayTime] as? NSNumber { | |
delayTimes.append(value) | |
} | |
totalTime = totalTime + (((gifDict?[kCGImagePropertyGIFDelayTime] as? NSNumber)?.floatValue)!) | |
} | |
var times = [AnyHashable](repeating: 0, count: 3) | |
var currentTime: Float = 0 | |
let count: Int = delayTimes.count | |
for i in 0..<count { | |
times.append(NSNumber(value: Float((currentTime / totalTime)))) | |
currentTime += Float(delayTimes[i]) | |
} | |
var images = [AnyHashable](repeating: 0, count: 3) | |
for i in 0..<count { | |
images.append(frames[i]) | |
} | |
animation.keyTimes = times as? [NSNumber] | |
animation.values = images | |
animation.timingFunction = CAMediaTimingFunction(name: .linear) | |
animation.duration = CFTimeInterval(totalTime) | |
animation.repeatCount = Float.infinity | |
animation.beginTime = AVCoreAnimationBeginTimeAtZero | |
animation.isRemovedOnCompletion = false | |
return animation | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment