osuka_
osuka_

Reputation: 485

Cannot create NSDictionary to define AVCaptureVideoDataOutput videoSettings

@Update 2: I was able to fix it using the Swift Dictionary literal.

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

First, quick disclaimer: this is the first real experience I'm having with code (so I'm sorry if this question is particularly stupid!), and I'm running Xcode 7, which is in beta, so I'd like to make clear that this could be a bug in the current build (7.0 beta 3 (7A152u)). I am, however, clueless - I don't know for sure if it's my fault or Xcode's. (that's why I'm asking instead of submitting a bug report - I'm not that confident)

That being said, I can't seem to define the videoSettings for my AVCaptureVideoDataOutput variable as I had done on Xcode 6. I've tried in quite a few different ways, most of which I've gotten from previous Stack Overflow questions, but none works. The application does not build on Xcode 7, but the code that ran on iOS 8, compiled on Xcode 6, also ran on iOS 9 after the update, without issues.

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

Here's everything I tried (could come up with/came across): (comments are 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 snippet of Obj-c code ("Stolen" from 1)

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

Now, given that I only have a basic notion of objective C, I might've misunderstood the code, but this is how my code turned out:

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

Thing is, there doesn't seem to be an initializer for NSString that accepts CFString

Upvotes: 3

Views: 2153

Answers (4)

i4guar
i4guar

Reputation: 677

In Swift 5 and Xcode 12, only the following works:

videoDataOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String: Int(kCVPixelFormatType_32BGRA)]

Upvotes: 0

Ankit Jain
Ankit Jain

Reputation: 1886

Working answer is here,

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

Check the answer give by @Leo at https://stackoverflow.com/a/29786129/1630208

Upvotes: 2

coyer
coyer

Reputation: 4367

This should be now:

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

Upvotes: 1

gnasher729
gnasher729

Reputation: 52566

You can't put an Int into an 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)

Upvotes: 1

Related Questions