2

attachments is a CFDictionaryRef. How do I accomplish the (_bridge NSDictionary *) functionality in Swift?

CIImage *ciImage = [[CIImage alloc] initWithCVPixelBuffer:pixelBuffer 
                                                  options:(__bridge NSDictionary *)attachments];

UPDATE

here is the full code section I have tried for creating the CIImage.

func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {

    var pixelBuffer:CVPixelBufferRef = CMSampleBufferGetImageBuffer(sampleBuffer)
    var attachmentMode = CMAttachmentMode(kCMAttachmentMode_ShouldPropagate)
    var attachments = CMCopyDictionaryOfAttachments(kCFAllocatorDefault, sampleBuffer, attachmentMode)
    var ciImage:CIImage = CIImage(CVPixelBuffer: pixelBuffer, options: attachments)

}

ANSWER

@NobodyNada's answer is correct, but because attachments is an 'unmanaged' CFDictionary you have to take the unretainedValue of the dictionary in order to clear the error. The correct answer is:

var ciImage:CIImage = CIImage(CVPixelBuffer: pixelBuffer, options: attachments.takeUnretainedValue())

1 Answer 1

4

That is called toll-free bridging, and it allows you to convert between certain Foundation and CoreFoundation types with a simple cast. The __bridge thing was added with ARC because without it, ARC couldn't figure out enough information about it. NSDictionaries and CFDictionaries are interchangeable in Swift without a cast:

let ciImage = CIImage(buffer: pixelBuffer, options: attachments).takeUnretainedValue()

P. S. Hi again:) Sorry I couldn't answer your other question; I had to go suddenly.

8
  • Hello! haha...when I use the code you've provided I get the error "extra argument 'options' in call". I think it may be because attachments is an unmanaged CFDictionaryRef. That's why I thought I needed some sort of bridging functionality Commented Mar 30, 2015 at 22:37
  • I've included how I create attachments in my original question Commented Mar 30, 2015 at 22:37
  • Oh wait, you fixed it too!!
    – NobodyNada
    Commented Mar 30, 2015 at 22:48
  • I think I have figured out the issue. Your answer is correct, however because attachments is 'unmanaged' you have to take the unretained value of it. I've posted what I believe is the correct solution above. Thanks again for your help and thorough explanation! Commented Mar 30, 2015 at 22:48
  • I'm pretty sure you are going to want to use takeRetainedValue() to avoid a memory leak, but I'm a bit rusty on CF reference counting rules. I'm looking it up right now...
    – NobodyNada
    Commented Mar 30, 2015 at 22:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.