2

我有一个小的整数值,我想将其转换为 CMTime。

问题是

CMTime(值: _ , timeScale: _)

或者

CMTimeMakeWithSeconds(值:_,timeScale:_)

将始终返回地板,以便时间始终等于0.0 seconds

let smallValue = 0.0401588716
let frameTime = CMTime(Int64(smallValue) , timeScale: 1) 
//frameTime is 0.0 seconds because of Int64 conversion

let frameTimeInSeconds = CMTimeMakeWithSeconds(smallValue , timeScale: 1) 
// frameTimeInSeconds also returns 0.0 seconds.
4

2 回答 2

1

CMTime将时间值表示为具有整数分子 (the ) 和分母 (the ) 的有理数。为了表示像您这样的小值,您必须选择更大的时间刻度(取决于所需的精度)。例子:valuetimescale

let smallValue = 0.0401588716
let frameTime = CMTime(seconds: smallValue, preferredTimescale: 1000000) 

print(frameTime.seconds) // 0.040158
于 2017-02-23T08:44:18.393 回答
1

在发布问题之前,我应该稍微考虑一下。

 let smallValue =  0.0401588716
 let oneSec =      CMTimeMakeWithSeconds(1, timeScale: 1) 
 let frameTime =   CMTimeMultiplyByFloat64(oneSec , smallValue)
 print(CMTimeGetSeconds(frameTime))                              //  0.040158872
于 2017-02-23T08:51:23.987 回答