Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix playback speed on non-60hz displays #180

Merged
merged 1 commit into from
Dec 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions SwiftyGif/UIImage+SwiftyGif.swift
Original file line number Diff line number Diff line change
Expand Up @@ -210,27 +210,21 @@ public extension UIImage {
let levelOfIntegrity = max(0, min(1, levelOfIntegrity))
var delays = delaysArray

var displayRefreshFactors = [Int]()

displayRefreshFactors.append(contentsOf: [60, 30, 20, 15, 12, 10, 6, 5, 4, 3, 2, 1])
var displayRefreshFactors = [60, 30, 20, 15, 12, 10, 6, 5, 4, 3, 2, 1]

// maxFramePerSecond,default is 60
let maxFramePerSecond = displayRefreshFactors[0]

// frame numbers per second
var displayRefreshRates = displayRefreshFactors.map { maxFramePerSecond / $0 }

if #available(iOS 10.3, *) {
if #available(iOS 10.3, tvOS 10.3, *) {
// Will be 120 on devices with ProMotion display, 60 otherwise.
// TODO: Use CADisplayLink to get current framerate. This is still 120 on ProMotion dispays even in low power mode.
let maximumFramesPerSecond = UIScreen.main.maximumFramesPerSecond
if maximumFramesPerSecond == 120 {
displayRefreshRates.append(maximumFramesPerSecond)
displayRefreshFactors.insert(maximumFramesPerSecond, at: 0)
}
}


let maxFramePerSecond = displayRefreshFactors[0]

// time interval per frame
let displayRefreshDelayTime = displayRefreshRates.map { 1 / Float($0) }
let displayRefreshDelayTime = displayRefreshFactors.map { Float($0) / Float(maxFramePerSecond) }

// calculate the time when each frame should be displayed at(start at 0)
for i in delays.indices.dropFirst() {
Expand Down