Getting Exposure Time (EXIF) from CMSampleBuffer

2.7k views Asked by At

I am trying to get the exposure time from an image captured using AVFoundation. When I followed the 2010's WWDC instruction about retrieving useful image metadata from CMSampleBuffer like this:

-(void)captureStillImageWithCompletion:(completion_exp)completionHandler
{
AVCaptureConnection *connection = [stillImage connectionWithMediaType:AVMediaTypeVideo];

typedef void(^MyBufBlock)(CMSampleBufferRef, NSError*);

MyBufBlock h = ^(CMSampleBufferRef buf, NSError *err){
    CFDictionaryRef exifAttachments = CMGetAttachment(buf, kCGImagePropertyExifDictionary, NULL);
    if(exifAttachments){
        NSLog(@"%@",exifAttachments);
    }

    if(completionHandler){
        completionHandler();
    }
};

[stillImage captureStillImageAsynchronouslyFromConnection:connection completionHandler:h];
}

I had an error on the CFDictionaryRef line:

Cannot initialize a variable of type'CFDictionaryRef (aka 'const __CFDictionary*') with an rvalue of type CFTypeRef..

So I followed a solution in the internet by casting it like this:

CFDictionaryRef exifAttachments = (CFDictionaryRef)CMGetAttachment(buf, kCGImagePropertyExifDictionary, NULL);

And now it gives me another error: Undefined symbols for architecture armv7s

(Apple Mach-o Linker Error: "_kCGImagePropertyExifDictionary", referenced from:)
(Apple Mach-o Linker Error: "_CMGetAttachment", referenced from:)

I don't understand what went wrong with my program. Anybody has any idea?

3

There are 3 answers

0
Laz On

EDIT: You have to include ImageIO library and header to make this key work. If you do not want to do that you can use this:

There's something about the key I think. This worked for me:

CFDictionaryRef exifAttachments = CMGetAttachment(buf, (CFStringRef)@"{Exif}", NULL);
0
Philli On

Struggling with that too, I found out that you can simply cast the Exif attachment as an NSDictionary. I hope this will help anyone.

let metadata = CMGetAttachment(image, "{Exif}", nil ) as! NSDictionary
let buf = metadata.valueForKey("ExposureTime") as! NSNumber
let xposure:Double = buf.doubleValue
0
Volodymyr Kulyk On

Modified version of Philli's answer for Swift4:

var exifEd: Double?
if let metadata = CMGetAttachment(sampleBuffer, key: "{Exif}" as CFString, attachmentModeOut: nil) as? NSDictionary {
    if let exposureDurationNumber = metadata.value(forKey: "ExposureTime") as? NSNumber {
        exifEd = exposureDurationNumber.doubleValue
    }
}