Skip to content

Commit

Permalink
feat: suppoer heics file
Browse files Browse the repository at this point in the history
  • Loading branch information
douknow committed Aug 15, 2023
1 parent 6b989c5 commit 2880f75
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
14 changes: 13 additions & 1 deletion Sources/Gifu/Classes/Animator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,15 @@ public class Animator {
private lazy var displayLink: CADisplayLink = { [unowned self] in
self.displayLinkInitialized = true
let display = CADisplayLink(target: DisplayLinkProxy(target: self), selector: #selector(DisplayLinkProxy.onScreenUpdate))
if #available(iOS 15.0, *) {
display.preferredFramesPerSecond = 60
// display.preferredFrameRateRange = .init(minimum: 1/120, maximum: 1/120)
}
display.isPaused = true
return display
}()

var lastTime: CFTimeInterval?

/// Introspect whether the `displayLink` is paused.
var isAnimating: Bool {
Expand Down Expand Up @@ -71,7 +77,13 @@ public class Animator {
return
}

store.shouldChangeFrame(with: displayLink.duration) {
var duration = displayLink.duration
if let lastTime {
duration = displayLink.targetTimestamp - lastTime
}
lastTime = displayLink.targetTimestamp

store.shouldChangeFrame(with: duration) {
if $0 {
delegate.animatorHasNewFrame()
if store.isLoopFinished, let loopBlock = loopBlock {
Expand Down
9 changes: 7 additions & 2 deletions Sources/Gifu/Classes/FrameStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ class FrameStore {
}

private let lock = NSLock()

var lastTime: Date?

/// Creates an animator instance from raw GIF image data and an `Animatable` delegate.
///
Expand Down Expand Up @@ -141,8 +143,11 @@ class FrameStore {
if currentFrameDuration > timeSinceLastFrameChange {
handler(false)
} else {
resetTimeSinceLastFrameChange()
incrementCurrentFrameIndex()
while timeSinceLastFrameChange > currentFrameDuration {
resetTimeSinceLastFrameChange()
incrementCurrentFrameIndex()
}

handler(true)
}
}
Expand Down
28 changes: 24 additions & 4 deletions Sources/Gifu/Helpers/ImageSourceHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,42 @@ func duration(withUnclampedTime unclampedDelayTime: Double, andClampedTime delay

/// An extension of `CGImageSourceRef` that adds GIF introspection and easier property retrieval.
extension CGImageSource {

var isTypeGIF: Bool {
UTTypeConformsTo(CGImageSourceGetType(self) ?? "" as CFString, kUTTypeGIF)
}

var isTypeHEICS: Bool {
UTTypeConformsTo(CGImageSourceGetType(self) ?? "" as CFString, "public.heics" as CFString)
}

/// Returns whether the image source contains an animated GIF.
///
/// - returns: A boolean value that is `true` if the image source contains animated GIF data.
var isAnimatedGIF: Bool {
let isTypeGIF = UTTypeConformsTo(CGImageSourceGetType(self) ?? "" as CFString, kUTTypeGIF)
let imageCount = CGImageSourceGetCount(self)
return isTypeGIF != false && imageCount > 1
return imageCount > 1
}

/// Returns the GIF properties at a specific index.
///
/// - parameter index: The index of the GIF properties to retrieve.
/// - returns: A dictionary containing the GIF properties at the passed in index.
func properties(at index: Int) -> GIFProperties? {
guard let imageProperties = CGImageSourceCopyPropertiesAtIndex(self, index, nil) as? [String: AnyObject] else { return nil }
return imageProperties[String(kCGImagePropertyGIFDictionary)] as? GIFProperties
CGImageSourceGetType(self)
if isTypeGIF {
guard let imageProperties = CGImageSourceCopyPropertiesAtIndex(self, index, nil) as? [String: AnyObject] else { return nil }
return imageProperties[String(kCGImagePropertyGIFDictionary)] as? GIFProperties
} else if isTypeHEICS {
guard let imageProperties = CGImageSourceCopyPropertiesAtIndex(self, index, nil) as? [String: AnyObject] else { return nil }
if #available(iOS 13.0, *) {
return imageProperties[String(kCGImagePropertyHEICSDictionary)] as? GIFProperties
} else {
return nil
}
} else {
return nil
}
}
}

Expand Down

0 comments on commit 2880f75

Please sign in to comment.