Skip to content

Commit

Permalink
Move memory extension
Browse files Browse the repository at this point in the history
  • Loading branch information
kaishin committed Jan 2, 2024
1 parent 92abae0 commit 74e3b86
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 deletions.
28 changes: 14 additions & 14 deletions Sources/Gifu/Classes/Animator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class Animator {
var loopDuration: TimeInterval {
return frameStore?.loopDuration ?? 0
}

/// Number of frame to buffer.
var frameBufferSize = 50

Expand Down Expand Up @@ -60,21 +60,21 @@ public class Animator {
/// Checks if there is a new frame to display.
fileprivate func updateFrameIfNeeded() {
guard let store = frameStore else { return }

if store.isFinished {
stopAnimating()
if let animationBlock = animationBlock {
animationBlock()
}
return
stopAnimating()
if let animationBlock = animationBlock {
animationBlock()
}
return
}

store.shouldChangeFrame(with: displayLink.duration) {
if $0 {
delegate.animatorHasNewFrame()
if store.isLoopFinished, let loopBlock = loopBlock {
loopBlock()
}
delegate.animatorHasNewFrame()
if store.isLoopFinished, let loopBlock = loopBlock {
loopBlock()
}
}
}
}
Expand All @@ -89,8 +89,8 @@ public class Animator {
/// - parameter completionHandler: Completion callback function
func prepareForAnimation(withGIFNamed imageName: String, inBundle bundle: Bundle = .main, size: CGSize, contentMode: UIView.ContentMode, loopCount: Int = 0, completionHandler: (() -> Void)? = nil) {
guard let extensionRemoved = imageName.components(separatedBy: ".")[safe: 0],
let imagePath = bundle.url(forResource: extensionRemoved, withExtension: "gif"),
let data = try? Data(contentsOf: imagePath) else { return }
let imagePath = bundle.url(forResource: extensionRemoved, withExtension: "gif"),
let data = try? Data(contentsOf: imagePath) else { return }

prepareForAnimation(withGIFData: data,
size: size,
Expand Down
24 changes: 13 additions & 11 deletions Sources/Gifu/Classes/FrameStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,6 @@ class FrameStore {
}
}

extension UIImage {
var memorySize: Int {
guard let cgImage = self.cgImage else { return 0 }
let instanceSize = MemoryLayout<UIImage>.size(ofValue: self)
let pixmapSize = cgImage.height * cgImage.bytesPerRow
let totalSize = instanceSize + pixmapSize
return totalSize
}
}


private extension FrameStore {
/// Optionally loads a single frame from an image source, resizes it if required, then returns an `UIImage`.
///
Expand Down Expand Up @@ -322,12 +311,25 @@ private extension FrameStore {
loadFrameAtIndexIfNeeded(index)
}

if #available(iOSApplicationExtension 15.0, *) {
print(#function, "Cache:", totalFrameCacheSize.formatted(.byteCount(style: .memory)))
}

self.loopDuration = duration
}

/// Reset animated frames.
func resetAnimatedFrames() {
animatedFrames = []
}

private var totalFrameCacheSize: Int {
animatedFrames
.filter({ !$0.isPlaceholder })
.compactMap(\.image)
.reduce(0) { size, image in
size + image.memorySize
}
}
}
#endif
9 changes: 9 additions & 0 deletions Sources/Gifu/Extensions/UIImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,14 @@ extension UIImage {
class func size(withImageData data: Data) -> CGSize? {
return UIImage(data: data)?.size
}

var memorySize: Int {
guard let cgImage = self.cgImage else { return 0 }
let instanceSize = MemoryLayout<UIImage>.size(ofValue: self)
let pixmapSize = cgImage.height * cgImage.bytesPerRow
let totalSize = instanceSize + pixmapSize
return totalSize
}
}
#endif

0 comments on commit 74e3b86

Please sign in to comment.