转换[NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA],(id)kCVPixelBufferPixelFormatTypeKey,nil];到斯威夫特

时间:2015-04-21 23:53:59

标签: objective-c swift

在此处收到错误消息。绞尽脑汁。尝试过各种各样的组合。

  

找不到类型为'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)

3 个答案:

答案 0 :(得分:11)

更新: Xcode 7.0•Swift 2.0

videoOutput.videoSettings = NSDictionary(object: Int(kCVPixelFormatType_32BGRA), forKey: kCVPixelBufferPixelFormatTypeKey as String) as [NSObject : AnyObject]

或只是

videoOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey : Int(kCVPixelFormatType_32BGRA)]

答案 1 :(得分:0)

看起来类型为[String: Int]

实际上,查看Xcode文档时,它表示值可以是单个数字,也可以是数组。如果它是一个数组,那么类型将是[String: [Int]]

所以你应该说:

  

让aVideoSetting:[String:Int] =
  [kCVPixelBufferPixelFormatTypeKey:kCVPixelFormatType_32BGRA]   videoOutput.videoSettings = aVideoSetting

我仍然不是100%与Cocoa类型和Swift类型的互操作性,所以这可能不完美,但它已经接近。

您还可以重写代码以使用swift语法创建NSDictonary:

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

答案 2 :(得分:0)

这已在 Swift 4.x 中更改,以下内容适用于我:

captureVideoDataOutput.videoSettings = 
    [kCVPixelBufferPixelFormatTypeKey as String : kCVPixelFormatType_32BGRA]