CMTime / CMTimeMake noob问题

时间:2011-05-14 05:21:43

标签: ios

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总是为零?

1 个答案:

答案 0 :(得分:3)

NSLog的格式字符串存在问题。由于你的问题标题表明你是“ noob ”,我会花一些时间来解释这里发生了什么。

使用可变数量的参数(如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

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

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

相关问题