ios - 将 CMTime 值转换为 swift

标签 ios swift cmtime

我有以下 2 行代码正在迁移到 Swift,但我有点卡住了。

CMTime trimmingTime = CMTimeMake(lround(videoAsset.naturalTimeScale / videoAsset.nominalFrameRate), videoAsset.naturalTimeScale);
CMTimeRange timeRange = CMTimeRangeMake(trimmingTime, CMTimeSubtract(videoAsset.timeRange.duration, trimmingTime));

将以下行转换为开头时出现以下错误。

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'

我尝试了几种不同的方法,但似乎没有任何效果。

最佳答案

您不能像其他语言那样简单地在 swift 中对不同类型的操作数执行数学运算。您需要手动进行类型转换。

在这里,您应该将 videoAsset.naturalTimeScale(即 CMTimeScale 并且 CMTimeScale 的类型为 Int32)转换为 Float 以使其工作。

Float(videoAsset.naturalTimeScale)

但是 CMTimeMake 的值键将接受值 CMTimeValue 类型的值。所以像这样使用它:

trimmingTime = CMTimeMake(value: CMTimeValue(Float(videoAsset.naturalTimeScale) / videoAsset.nominalFrameRate), timescale: videoAsset.naturalTimeScale)

再次让你的代码更敏捷使用 CMTime 而不是 CMTimeMake 作为:

trimmingTime = CMTime(value: CMTimeValue(Float(videoAsset.naturalTimeScale) / videoAsset.nominalFrameRate), timescale: videoAsset.naturalTimeScale)

关于ios - 将 CMTime 值转换为 swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52736734/

相关文章:

ios - 解析本地数据存储和 PFObject 子类

iOS:使用 sqlite3 查询瑞典字母时出现问题

ios - 如何在 SwiftUI 中更改 DatePicker 的大小

ios - 无法将 Swift CMTimeRange 转换为 Float 或获取数字?

ios - avplayer 在 seekToTime 上完成歌曲

iphone - 使用 uiwebview 创建一个富文本 uitableviewcell

ios - 屏幕方向更改后页面右侧的空白区域

ios - 无法更改 SplitViewController 中状态栏的颜色

ios - UIPopoverPresentationController 显示为全窗口弹出

objective-c - 在 objective-c 中将 CMTime 转换为人类可读时间