[英]Converting CMTime values to swift
I have the following 2 lines of code that I am moving to Swift but I am a little stuck. 我有以下两行代码,我将移至Swift,但有些卡住了。
CMTime trimmingTime = CMTimeMake(lround(videoAsset.naturalTimeScale / videoAsset.nominalFrameRate), videoAsset.naturalTimeScale);
CMTimeRange timeRange = CMTimeRangeMake(trimmingTime, CMTimeSubtract(videoAsset.timeRange.duration, trimmingTime));
When converting the below line to begin with I get the following error. 当转换下面的行开始时,出现以下错误。
var trimmingTime: CMTime
trimmingTime = CMTimeMake(value: lround(videoAsset.naturalTimeScale / videoAsset.nominalFrameRate), timescale: videoAsset.naturalTimeScale)
Binary operator '/' cannot be applied to operands of type 'CMTimeScale' (aka 'Int32') and 'Float'
二进制运算符“ /”不能应用于类型为“ CMTimeScale”(又名“ Int32”)和“ Float”的操作数
I have tried a few different approaches but nothing seems to work. 我尝试了几种不同的方法,但是似乎没有任何效果。
Binary operator '/' cannot be applied to operands of type 'CMTimeScale' (aka 'Int32') and 'Float' so you need forecast CMTimeScale into Float.
二进制运算符“ /”不能应用于类型为“ CMTimeScale”(又名“ Int32”)和“ Float”的操作数,因此您需要将CMTimeScale预测为Float。
You need to convert CMTimeScale
into Float
: 您需要将
CMTimeScale
转换为Float
:
trimmingTime = CMTimeMake(value: lround(Float(videoAsset.naturalTimeScale) / videoAsset.nominalFrameRate), timescale: videoAsset.naturalTimeScale)
You can not simply perform mathematically operation with different type of operands in swift like other languages. 您不能像其他语言一样快速地对不同类型的操作数进行数学运算。 You need to type-cast manually.
您需要手动输入类型。
Here you should cast videoAsset.naturalTimeScale
( which is CMTimeScale
and CMTimeScale is of type Int32
) to Float to get it work. 在这里,您应该将
videoAsset.naturalTimeScale
(这是CMTimeScale
而CMTimeScale的类型是Int32
)强制转换为Float才能正常工作。
Float(videoAsset.naturalTimeScale)
But CMTimeMake
's value key will accept value CMTimeValue
type value. 但是
CMTimeMake
的value键将接受CMTimeValue
类型的值。 So use it like: 所以像这样使用它:
trimmingTime = CMTimeMake(value: CMTimeValue(Float(videoAsset.naturalTimeScale) / videoAsset.nominalFrameRate), timescale: videoAsset.naturalTimeScale)
Again to make you code more Swifty use CMTime
rather CMTimeMake
as: 再次使您的代码更快速,使用
CMTime
而不是CMTimeMake
作为:
trimmingTime = CMTime(value: CMTimeValue(Float(videoAsset.naturalTimeScale) / videoAsset.nominalFrameRate), timescale: videoAsset.naturalTimeScale)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.