user3353890
user3353890

Reputation: 1891

How do I convert this to Swift?

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())

Upvotes: 2

Views: 1177

Answers (1)

NobodyNada
NobodyNada

Reputation: 7634

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.

Upvotes: 4

Related Questions