如何将Int32值转换为CGFloat?

这里我的代码

let width = CMVideoFormatDescriptionGetDimensions(device.activeFormat.formatDescription as CMVideoFormatDescriptionRef!)。width – return Int32

let height = CMVideoFormatDescriptionGetDimensions(device.activeFormat.formatDescription as CMVideoFormatDescriptionRef!)。height – return Int32

myLayer?.frame = CGRectMake(0,0,width,height)

错误是’Int32’不能转换为CGFloat,如何将Int32转换为CGFloat?

要在数值数据类型之间进行转换,将创建一个新的目标类型实例,将源值作为参数传递。所以要将Int32转换为CGFloat:
let int: Int32 = 10
let cgfloat = CGFloat(int)

在你的情况下,你可以做:

let width = CGFloat(CMVideoFormatDescriptionGetDimensions(device.activeFormat.formatDescription as CMVideoFormatDescriptionRef!).width)
let height = CGFloat(CMVideoFormatDescriptionGetDimensions(device.activeFormat.formatDescription as CMVideoFormatDescriptionRef!).height)

myLayer?.frame = CGRectMake(0,width,height)

要么:

let width = CMVideoFormatDescriptionGetDimensions(device.activeFormat.formatDescription as CMVideoFormatDescriptionRef!).width
let height = CMVideoFormatDescriptionGetDimensions(device.activeFormat.formatDescription as CMVideoFormatDescriptionRef!).height

myLayer?.frame = CGRectMake(0,CGFloat(width),CGFloat(height))

请注意,swift中的数字类型之间没有隐式或显式类型转换,所以您必须使用相同的模式,也可以将Int转换为Int32或UInt等。

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...