繁体   English   中英

CMTime / CMTimeMake noob问题

[英]CMTime / CMTimeMake noob question

CMTimeMake没有给我我期望的结果。 以下代码:

CMTime testTime = CMTimeMake(0, 30);
NSLog(@"testTime w/ input 0, 30: value: %d, timescale %d, seconds: %f",
       testTime.value, testTime.timescale,
      (float) testTime.value / testTime.timescale);

testTime = CMTimeMake(1, 30);
NSLog(@"testTime w/ input 1, 30: value: %d, timescale %d, seconds: %f",
      testTime.value, testTime.timescale,
      (float) testTime.value / testTime.timescale);

testTime = CMTimeMake(15, 30);
NSLog(@"testTime w/ input 15, 30: value: %d, timescale %d, seconds: %f",
      testTime.value, testTime.timescale,
      (float) testTime.value / testTime.timescale);

产生以下输出:

testTime w / input 0,30:value:0,timescale 0,seconds:0.000000

testTime w / input 1,60:值:1,时间刻度0,秒:0.000000

testTime w / input 15,60:value:15,timescale 0,seconds:0.000000

为什么testTime.timescale总是为零?

这是NSLog的格式字符串的问题。 既然你的问题标题表明你是一个“ 菜鸟 ”,我会花一些时间来解释这里发生了什么。

采用可变数量的参数(如NSLog(NSString* format, ...)的函数需要根据格式字符串读取额外的参数...

  • %d表示:读取四个字节(32位)并将其视为十进制整数。
  • %f表示:读取四个字节(32位)并将其视为浮点数。

我们来看看你的最后一个例子:

您在格式字符串中传递%d %d %f ,后跟:

testTime.value     // A 64-bit integer (8 bytes) with the value 15
testTime.timescale // A 32-bit integer (4-bytes) with the value 30
(float)15 / 30     // A 32-bit float (4-bytes) with the value 0.5f

由于这些数字传入的方式,你最终读取第一个%d的最低有效32位testTime.value ,正好被解释为15,然后是第二个%d%f你是读取高32位(0)和可能的一些填充字节得到0.0。 我实际上有点疑惑为什么你得到0.0而不是一些小数字,因为我希望30被解释为一个浮点数,这将是4.2E-44 - 如果有人知道请让我知道。

无论如何,解决它的方法是将第一个%d更改为%lld ,这将正确显示值。 testTime变量实际上一直保持正确的值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM