Cannot create NSDictionary for AVCaptureVideoDataOutput videoSettings definition

@Update 2: I was able to fix this using a Swift literal Dictionary

.

videoDataOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as NSString: NSNumber(unsignedInt: kCVPixelFormatType_32BGRA)]

      


First, a quick disclaimer: this is the first real experience I have with the code (so I'm sorry if this question is particularly stupid!) And I'm running Xcode 7, which is in beta, so I wanted to to clarify that this may be a bug in the current build (7.0 beta 3 (7A152u)). I am, however, clueless - I don't know for sure if it is my fault or Xcode. (which is why I am asking instead of sending a bug report - I am not sure about that)

As I said, I cannot define the videoSettings settings for my AVCaptureVideoDataOutput variable as I did in Xcode 6. I tried several different ways, most of which I got from the previous questions, but none seem to work. The app is not built on Xcode 7, but code that ran on iOS 8 compiled on Xcode 6 also ran on iOS 9 after the upgrade without issue.

I've looked at the Apple documentation and nothing seems to have changed and I can't find any useful code either. I have checked the AVCaptureVideoDataOutput, AVFoundation, NSDictionary and NSObject pages to no avail.

Here's everything I tried (can come up with / came across): (comments - compiler errors)

videoDataOutput = AVCaptureVideoDataOutput()

videoDataOutput.videoSettings = NSDictionary(objectsAndKeys: Int(kCVPixelFormatType_32BGRA), (kCVPixelBufferPixelFormatTypeKey))
//Cannot find an initializer for type 'NSDictionary that accepts an argument list of type '(objectsAndKeys: Int, (CFString))'

videoDataOutput.videoSettings = NSDictionary(objects: Int(kCVPixelFormatType_32BGRA), forKeys: (kCVPixelBufferPixelFormatTypeKey))
//Missing argument for parameter 'count' in call

videoDataOutput.videoSettings = NSDictionary(objects: Int(kCVPixelFormatType_32BGRA), forKeys: (kCVPixelBufferPixelFormatTypeKey)) as [AnyObject : AnyObject]
//Type 'AnyObject' does not conform to protocol 'Hashable'

videoDataOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey:kCVPixelFormatType_32BGRA]
//'_' is not convertible to 'CFString'

      

So what I need to know is: Am I doing something wrong? If so, what is it and how can I fix the code? If not, what exactly should I include in the bug report?


@Update

During my endless search I came across this Obj-c code snippet ("Stolen" from 1 )

dataOutput.videoSettings = [NSDictionary dictionaryWithObject:[NSNumber numberWithUnsignedInt:kCVPixelFormatType_420YpCbCr8BiPlanarFullRange] forKey:(NSString *)kCVPixelBufferPixelFormatTypeKey];

      

Now, given that I only have the basic concept of a C lens, I might misunderstand the code, but it just so happens that my code is:

videoDataOutput.videoSettings = NSDictionary(object: NSNumber(unsignedInt: kCVPixelFormatType_32BGRA), forKey: NSString(kCVPixelBufferPixelFormatTypeKey))

      

Thing there seems to be no initializer for NSString that accepts CFString

+3


source to share


3 answers


A working answer is here.

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

      



Check out the answer provided by @Leo at fooobar.com/questions/1583884 / ...

+2


source


You cannot put Int into NSDictionary.

To create an NSDictionary in Swift with multiple objects and keys:

NSDictionary(objectsAndKeys: [obj1, key1, obj2, key2])

      



One object and key

NSDictionary(object, forKey: key)

      

+1


source


It should now be:

videoDataOutput.videoSettings = NSDictionary(object: NSNumber(unsignedInt: kCVPixelFormatType_32BGRA), forKey: NSString(string: kCVPixelBufferPixelFormatTypeKey)) as [NSObject : AnyObject]

      

+1


source







All Articles