Created
October 17, 2023 03:06
-
-
Save awxkee/247e81624c5492e1c5d28122a55a5218 to your computer and use it in GitHub Desktop.
Avif animated gist
This file contains hidden or 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
| 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