6

Here was my workable code in the previous version of Swift:

 let imageOptionsDictKeys = [ kCVPixelBufferPixelFormatTypeKey, kCVPixelBufferWidthKey,  kCVPixelBufferHeightKey, kCVPixelBufferOpenGLESCompatibilityKey, kCVPixelBufferIOSurfacePropertiesKey]
 let imageOptionsDictValues = [ cvPixelFormatType,  frameW, frameH, boolYES]

 var keyCallbacks = kCFTypeDictionaryKeyCallBacks
 var valueCallbacks = kCFTypeDictionaryValueCallBacks

 let imageOptions = CFDictionaryCreate(kCFAllocatorDefault, UnsafeMutablePointer(imageOptionsDictKeys), UnsafeMutablePointer(imageOptionsDictValues), 4, &keyCallbacks, &valueCallbacks)

After changes in the Swift 3.0 I have to convert my keys and values arrays into UnsafeMutablePointer<UnsafeRawPointer?> for creating CFDictionary.

This way:

    let imageOptionsDictKeysPointer =  UnsafeMutablePointer<UnsafeRawPointer?>.allocate(capacity: 1)
    imageOptionsDictKeysPointer.initialize(to: imageOptionsDictKeys)

gives an Bad Access error.

And after reading documentation I am trying to compile this code:

        let imageOptionsDictKeys = [kCVPixelBufferPixelFormatTypeKey, kCVPixelBufferWidthKey,  kCVPixelBufferHeightKey, kCVPixelBufferOpenGLESCompatibilityKey]
        let imageOptionsDictKeysRawPointer = Unmanaged.passUnretained(imageOptionsDictKeys).toOpaque()
        let imageOptionsDictKeysPointer =  UnsafeMutablePointer<UnsafeRawPointer?>.allocate(capacity: 1)
        imageOptionsDictKeysPointer.initialize(to: imageOptionsDictKeysRawPointer)

        let imageOptionsDictValues = [ cvPixelFormatType,  frameW, frameH, boolYES]
        let imageOptionsDictValuesRawPointer = Unmanaged.passUnretained(imageOptionsDictValues).toOpaque()
        let imageOptionsDictValuesPointer =  UnsafeMutablePointer<UnsafeRawPointer?>.allocate(capacity: 1)
        imageOptionsDictValuesPointer.initialize(to: imageOptionsDictValuesRawPointer)

        let imageOptions = CFDictionaryCreate(kCFAllocatorDefault, imageOptionsDictKeysPointer, imageOptionsDictValuesPointer, 4, &keyCallbacks, &valueCallbacks)

but error Generic parameter 'Instance' could not be inferred appears in the lines Unmanaged.passUnretained(array).toOpaque()

I have no idea how to create CFDictionary programmatically now.

JULIIncognito
  • 1,875
  • 2
  • 15
  • 15
  • Don't create a CFDictionary. Create a Swift Dictionary instead, as for example here: http://stackoverflow.com/a/38171414/1187415. – Martin R Oct 10 '16 at 20:04
  • I have to call function VTCompressionSessionCreate(....) with CFDictionary params. I am not sure that I can to use just Swift Dictionary... or are they convertible somehow? – JULIIncognito Oct 10 '16 at 20:14
  • Seems like an unnecessarily complicated way of making a `CFDictionary`, and a textbook [XY Problem](http://xyproblem.info). See http://stackoverflow.com/q/29301302/3141234 – Alexander Oct 10 '16 at 20:15
  • For now I`ve just simplify the creation of CFDictionary. It looks like dictionary are created, but my next step still not working. Maybe it is because of this dictionary, I am not sure http://stackoverflow.com/questions/39987373/videotoolbox-does-not-create-an-encoder-session-for-mpeg4-in-swift-3-0 – JULIIncognito Oct 11 '16 at 22:00

1 Answers1

-1

I just solved a similar issue converting arrays to UnsafeMutablePointer< UnsafeMutablePointer<T>> which you can find here: Swift 3 UnsafeMutablePointer initialization for C type float**

To convert swift arrays using the same scheme, use UnsafeMuTablePointer as suggested here: http://technology.meronapps.com/2016/09/27/swift-3-0-unsafe-world-2/

Community
  • 1
  • 1
Arshia
  • 334
  • 2
  • 13