这里我的代码,
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等。