3

@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

osuka_
  • 484
  • 5
  • 15
  • Well, I am having the same problem. I think this is due to the Beta version of Xcode 7. – Julien LANGE Aug 11 '15 at 16:39
  • @osuka_ did you get any solution ? Below is code I'm trying and error i get is "Cannot find an initializer for type 'NSNumber' that accepts an argument list of type '(unsignedInt: Int)'" `let key = kCVPixelBufferPixelFormatTypeKey as NSString let value = NSNumber(unsignedInt: kCVPixelFormatType_32BGRA) let videoSettings = NSDictionary(objects: value, forKeys: key) videoFileOutput?.videoSettings = videoSettings` – Ankit Jain Dec 29 '15 at 09:38

4 Answers4

2

Working answer is here,

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

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

Community
  • 1
  • 1
Ankit Jain
  • 1,858
  • 22
  • 35
1

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)
gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • How should I add the pixel format type, then? It doesn't work as OSType either (without converting to Int) in any of the four cases. (And this code worked/works on Xcode6) – osuka_ Jul 11 '15 at 22:13
  • Nope, still nothing. I've tried a couple things: `videoDataOutput.videoSettings = (NSDictionary(objects: NSNumber(kCVPixelFormatType_32BGRA), forKeys: kCVPixelBufferPixelFormatTypeKey))`, the last working line with NSNumber would be the next logical step, but instead gives me two errors. Turns out I need to label `kCVPixelFormatType_32BGRA` as an unsigned int. Xcode also suggests the addition of `as! [NSObject : AnyObject]` to downcast (due to NSDictionary not being convertible to [NSObject : AnyObject]), but adding the code would result in another error – osuka_ Jul 12 '15 at 01:31
  • Sorry for the double comment, but I broke the character limit. To be more specific, Xcode now "Cannot assign a value of type '[NSObject : AnyObject]' to a value of type '[NSObject : AnyObject]!'"; adding an exclamation mark to NSDictionary gives me a "missing argument for parameter 'count' in call" error. The line after Xcode's suggestions is `videoDataOutput.videoSettings = (NSDictionary(objects: NSNumber(unsignedInt: kCVPixelFormatType_32BGRA), forKeys: kCVPixelBufferPixelFormatTypeKey)) as! [NSObject : AnyObject]` – osuka_ Jul 12 '15 at 01:34
1

This should be now:

videoDataOutput.videoSettings = NSDictionary(object: NSNumber(unsignedInt: kCVPixelFormatType_32BGRA), forKey: NSString(string: kCVPixelBufferPixelFormatTypeKey)) as [NSObject : AnyObject]
coyer
  • 4,122
  • 3
  • 28
  • 35
0

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

videoDataOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String: Int(kCVPixelFormatType_32BGRA)]
i4guar
  • 599
  • 5
  • 10