swift 转换[NS编号具有无符号整数的编号:kCV像素格式类型_32BGRA],(id)kCV像素缓冲器像素格式类型密钥,无];致斯威夫特

sczxawaw  于 2023-01-04  发布在  Swift
关注(0)|答案(3)|浏览(140)

这里出错了。我绞尽脑汁。尝试了各种组合。
找不到类型“NSDictionary”的初始值设定项,该初始值设定项接受类型“(object:(Int),forKey:CFString!)'

// configure the pixel format -  Obj-C
// videoOutput.videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA], (id)kCVPixelBufferPixelFormatTypeKey, nil];

// Swift       
videoOutput.videoSettings = NSDictionary(object: Int(kCVPixelFormatType_32BGRA), forKey:kCVPixelBufferPixelFormatTypeKey)
tyu7yeag

tyu7yeag1#

更新:Swift 5.7

videoOutput.videoSettings = [
    kCVPixelBufferPixelFormatTypeKey as String: Int(kCVPixelFormatType_32BGRA)
]
1zmg4dgp

1zmg4dgp2#

Swift 4.x中对此进行了更改,以下内容对我有效:

captureVideoDataOutput.videoSettings = 
    [kCVPixelBufferPixelFormatTypeKey as String : kCVPixelFormatType_32BGRA]
7kqas0il

7kqas0il3#

看起来类型应该是[String: Int]
实际上,在Xcode文档中,它说值可以是单个数字或数组,如果是数组,则类型为[String: [Int]]
所以你应该可以说:
输入视频设置:[字符串:整数] =
[kCV像素缓冲区像素格式类型密钥:kCVPixelFormatType_32BGRA]视频输出。视频设置= a视频设置
我仍然不是100%支持可可类型和Swift类型的互操作性,所以这可能不是完美的,但已经很接近了。
您还可以重写代码,以swift语法创建NSDictonary:

let aPixelFormat = NSNumber(int: kCVPixelFormatType_32BGRA)
let settingsDict = NSDictionary(aPixelFormat 
  forKey:  "numberWithUnsignedInt:kCVPixelFormatType_32BGRA")

相关问题