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/输入0,30:值: 0,时间刻度0,秒: 0.000000
testTime w/输入1,60:值: 1,时间刻度0,秒: 0.000000
testTime w/输入15,60:值: 15,时间刻度0,秒: 0.000000
为什么testTime.timescale总是零?
发布于 2012-05-28 22:27:18
这是NSLog格式字符串的一个问题。既然你的题目表明你是个“菜鸟”,我会花些时间来解释一下这里发生了什么。
使用可变数量的参数(如NSLog(NSString* format, ...) )的函数需要根据格式字符串读取额外的参数.
%d的意思是:读取4个字节(32位),并将其视为十进制integer.%f的意思:读取4个字节(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的最不重要的32位,它恰好被正确解释为15,然后对于第二个%d和%f,您将读取上面的32位(0),并且可能会读取一些填充字节以获得0.0。我实际上有点困惑,为什么你得到的是0.0,而不是一些小数字,因为我希望30被解释为一个浮动,这将是4.2E-44 -如果有人知道,请告诉我。
无论如何,解决这个问题的方法是将第一个%d转换为%lld,这将正确地显示值。实际上,testTime变量始终保持正确的值。
https://stackoverflow.com/questions/5999975
复制相似问题