ios - 如何在 iOS 应用程序中设置所需的渲染频率 (FPS)?

标签 ios objective-c cadisplaylink

我在带有 renderOneFrame 方法/处理程序的 iOS 应用程序上使用 CADisplayLink。我的理解是这是每次屏幕刷新时触发的,即 60Hz。

但是我的应用很难以 60FPS 的速度运行,并且要添加更多功能,我担心在旧硬件上这是不可行的。与其让它以可变速率运行,我宁愿以 30FPS 渲染并给自己更多的喘息空间。

有没有办法让这件事轻松发生?

这似乎是它的核心,但我是一个移植到 iOS 的 C++ 编码器,所以它有点困惑(我从我找到的示例中复制它):

- (void)go {

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    mLastFrameTime = 1;
    mStartTime = 0;

    try {
        app = new MyApp()
    } catch( Ogre::Exception& e ) {
        std::cerr << "An exception has occurred: " <<
        e.getFullDescription().c_str() << std::endl;
    }

    mDate = [[NSDate alloc] init];
    mLastFrameTime = -[mDate timeIntervalSinceNow];

    mDisplayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(renderOneFrame:)];
    [mDisplayLink setFrameInterval:mLastFrameTime];
    [mDisplayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

    [pool release];
}

最佳答案

这是 Apple 文档中关于 CADisplayLink 属性 frameInterval 的描述:

The default value is 1, which results in your application being notified at the refresh rate of the display. If the value is set to a value larger than 1, the display link notifies your application at a fraction of the native refresh rate. For example, setting the interval to 2 causes the display link to fire every other frame, providing half the frame rate.

Setting this value to less than 1 results in undefined behavior and is a programmer error.

在你的例子中,你很可能将帧间隔设置为一个非常小的数字,很容易小于 1。如果你想让它更新,你应该将它设置为 1每次刷新屏幕。否则,您应该将其设置为 2 以获得一半。

但是请注意,它没有具体提及帧速率是什么。据我所知,所有当前和现有的 iOS 设备的屏幕刷新率为 60 Hz,但没有什么可说的,Apple 不会有一天发布刷新率为 120 Hz 的设备。所以现在,将 frameInterval 设置为 2 将产生 30 fps 的帧速率,但在某些 future 的设备上它可能会产生 60 fps 的帧速率。无论如何,该设备的速度可能足以以该帧速率处理您的绘图代码,但这只是一个引用。此外,似乎没有任何方法可以通过编程方式仅检索该帧速率值,但您始终可以测量绘制帧之间的时间以查看帧之间的时间间隔,并进行一些数学计算以获得帧速率。

关于ios - 如何在 iOS 应用程序中设置所需的渲染频率 (FPS)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21289761/

相关文章:

ios - 在 UIScrollView 内的 UIView 上绘制草图

ios - 我可以使用 10ms drawRect 获得 60fps 吗?

ios - 将 CADisplayLink.timestamp CFTimeInterval 转换为主机时间 (uint64)

ios - 提高 Tesseract OCR 质量失败

ios - SwiftUI 中的 TextField 和 SecureField 之间是否存在高度差异?

objective-c - 如何确定 NSFont 是否安装在 Objective-C 的 Mac OSX 机器上?

ios - Xcode - 在我的应用程序上启用 iTunes 文件共享,但它没有出现在 iTunes 的列表中?

ios - 如何暂停 CADisplayLink?

ios - 使用 nib 的自定义初始化程序?

objective-c - 如何使函数在 View 出现时运行,但在警告框关闭时不运行?