Make CMTime with a very small value

China☆狼群 提交于 2019-12-02 02:08:44

问题


I have a small integer value and I want to convert it into CMTime.

The problem is that

CMTime(value: _ , timeScale: _)

or

CMTimeMakeWithSeconds(value: _ , timeScale: _)

will always return the floor so that time always equals 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.

回答1:


CMTime represents a time value as a rational number with integer numerator (the value) and denominator (the timescale) . In order to represent a small value like yours, you have to choose a larger timescale (depending on the desired accuracy). Example:

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

print(frameTime.seconds) // 0.040158



回答2:


I should have put a little thought into it before posting the question.

 let smallValue =  0.0401588716
 let oneSec =      CMTimeMakeWithSeconds(1, timeScale: 1) 
 let frameTime =   CMTimeMultiplyByFloat64(oneSec , smallValue)
 print(CMTimeGetSeconds(frameTime))                              //  0.040158872


来源:https://stackoverflow.com/questions/42410190/make-cmtime-with-a-very-small-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!