Skip to content

Commit

Permalink
Cache decoded frames in animated webp to boost performance
Browse files Browse the repository at this point in the history
  • Loading branch information
yeatse committed Jan 13, 2024
1 parent 9b539b8 commit 7ce241a
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions Sources/Image+WebP.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class WebPFrameSource: ImageFrameSource {
let data: Data?
private let decoder: WebPDecoderRef
private var decoderLock: UnsafeMutablePointer<os_unfair_lock>
private var frameCache = NSCache<NSNumber, CGImage>()

var frameCount: Int {
get {
Expand All @@ -115,9 +116,14 @@ class WebPFrameSource: ImageFrameSource {
defer {
os_unfair_lock_unlock(decoderLock)
}
guard let image = WebPDecoderCopyImageAtIndex(decoder, Int32(index)) else {
return nil
var image = frameCache.object(forKey: index as NSNumber)
if image == nil {
image = WebPDecoderCopyImageAtIndex(decoder, Int32(index))
if image != nil {
frameCache.setObject(image!, forKey: index as NSNumber)
}
}
guard let image = image else { return nil }
if let maxSize = maxSize, maxSize != .zero, CGFloat(image.width) > maxSize.width || CGFloat(image.height) > maxSize.height {
let scale = min(maxSize.width / CGFloat(image.width), maxSize.height / CGFloat(image.height))
let destWidth = Int(CGFloat(image.width) * scale)
Expand Down

0 comments on commit 7ce241a

Please sign in to comment.