Sirop4ik
Sirop4ik

Reputation: 5243

How to get image metadata using AVFoundation?

I am new in iOS and issue is I need to get metadata from image that I took

I work with my custom camera using AVFoundation

there is code how I get image

// Take picture callback
func capture(_ captureOutput: AVCapturePhotoOutput,
             didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?,
             previewPhotoSampleBuffer: CMSampleBuffer?,
             resolvedSettings: AVCaptureResolvedPhotoSettings,
             bracketSettings: AVCaptureBracketedStillImageSettings?,
             error: Error?) {

    if let error = error {
        appDeligate.log.error("ERROR occure : \(error.localizedDescription)")
    }

    if  let sampleBuffer = photoSampleBuffer,
        let previewBuffer = previewPhotoSampleBuffer,
        let dataImage = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: sampleBuffer, previewPhotoSampleBuffer: previewBuffer) {

        appDeligate.log.debug("dataImage : \(UIImage(data: dataImage)!.size as Any)")

        let dataProvider = CGDataProvider(data: dataImage as CFData)!
        let cgImageRef: CGImage! = CGImage(jpegDataProviderSource: dataProvider, decode: nil, shouldInterpolate: true, intent: .defaultIntent)
        let image = UIImage(cgImage: cgImageRef, scale: 1.0, orientation: UIImageOrientation.right)

        saveImageAndData(image: image)
    } else {
        appDeligate.log.error("some ERROR here")
    }
}

I found some explanation how to get metadata with UIImagePickerController, but there is any explanation how to do the same with AVFoundation

In Java I am using ExifInterface and then I can get properties such as

final String [] tagProperties = {TAG_DATETIME, TAG_DATETIME_DIGITIZED, TAG_EXPOSURE_TIME,
            TAG_FLASH, TAG_FOCAL_LENGTH, TAG_GPS_ALTITUDE, TAG_GPS_ALTITUDE_REF, TAG_GPS_DATESTAMP,
            TAG_GPS_LATITUDE, TAG_GPS_LATITUDE_REF, TAG_GPS_LONGITUDE, TAG_GPS_LONGITUDE_REF,
            TAG_GPS_PROCESSING_METHOD, TAG_GPS_TIMESTAMP, TAG_IMAGE_LENGTH, TAG_IMAGE_WIDTH, TAG_ISO, TAG_MAKE,
            TAG_MODEL, TAG_ORIENTATION, TAG_SUBSEC_TIME, TAG_SUBSEC_TIME_DIG, TAG_SUBSEC_TIME_ORIG, TAG_WHITE_BALANCE,
            TAG_FOCAL_LENGTH_IN_35MM_FILM, TAG_APERTURE_VALUE, TAG_BRIGHTNESS_VALUE, TAG_EXPOSURE_BIAS_VALUE,
            TAG_EXPOSURE_INDEX, TAG_F_NUMBER, TAG_ISO_SPEED_RATINGS, TAG_LIGHT_SOURCE, TAG_SHUTTER_SPEED_VALUE,
            TAG_SUBJECT_DISTANCE, TAG_SUBJECT_DISTANCE_RANGE};

How to get the same with Swift 3 ?

Thanks in advance!

Edit

according to the answer @Vimy, eventually I got output

key : PixelWidth, value : 3264
key : ColorModel, value : RGB
key : PixelHeight, value : 2448
key : {PNG}, value : {
Chromaticities =     (
    "0.3127",
    "0.329",
    "0.64",
    "0.33",
    "0.3",
    "0.6000000000000001",
    "0.15",
    "0.06"
);
Gamma = "0.45455";
InterlaceType = 0;
sRGBIntent = 0;
}
key : ProfileName, value : sRGB IEC61966-2.1
key : Depth, value : 8
************
key : BrightnessValue,  BRIGHT :: ** nil
************

There are all key - value pairs that image has as a property...

But according to the documentation there are have to be much more available properties

https://developer.apple.com/reference/imageio/cgimageproperties/exif_dictionary_keys

how to get access to all left properties?

Upvotes: 1

Views: 2064

Answers (1)

Vimy
Vimy

Reputation: 130

This helper function should do the trick.

 func getEXIFFromImage(image:NSData) -> NSDictionary {

    let imageSourceRef = CGImageSourceCreateWithData(image, nil);

    let currentProperties = CGImageSourceCopyPropertiesAtIndex(imageSourceRef!, 0, nil)

    let mutableDict = NSMutableDictionary(dictionary: currentProperties!)

    return mutableDict

}

You can then use the keys from CGImageProperties to access the metadata you want. https://developer.apple.com/reference/imageio/cgimageproperties/exif_dictionary_keys

Upvotes: 2

Related Questions