Skip to content

Instantly share code, notes, and snippets.

@awxkee
Created October 17, 2023 03:06
Show Gist options
  • Select an option

  • Save awxkee/247e81624c5492e1c5d28122a55a5218 to your computer and use it in GitHub Desktop.

Select an option

Save awxkee/247e81624c5492e1c5d28122a55a5218 to your computer and use it in GitHub Desktop.
Avif animated gist
import avifc
Task(priority: .high) {
let animationAvifUrl = Bundle.main.url(forResource: "animation_6", withExtension: "avif")!
var data = try! Data(contentsOf: animationAvifUrl)
let animationDecoder = AVIFAnimatedDecoder(data: data)!
let framesCount = animationDecoder.framesCount()
var currentFrame = framesCount
while (true) {
if currentFrame >= framesCount {
currentFrame = 0
}
let frameDuration = animationDecoder.frameDuration(currentFrame)
let frame = animationDecoder.getImage(currentFrame)
await MainActor.run {
animationImage = frame
}
print("\(frameDuration)")
try await Task.sleep(nanoseconds: NSEC_PER_MSEC * UInt64(frameDuration))
currentFrame += 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment