如何使用AVFoundation获取图像元数据?

时间:2017-01-29 12:14:04

标签: ios swift

我是iOS的新手,问题是我需要从我拍摄的图像中获取元数据

我使用AVFoundation

使用我的自定义相机

我有代码如何获取图像

// 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")
    }
}

我找到了一些如何使用UIImagePickerController获取元数据的解释,但有任何解释如何对AVFoundation

执行相同操作

在Java中我使用的是ExifInterface,然后我可以获得诸如

之类的属性
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};

如何使用Swift 3获得相同的效果?

提前致谢!

修改

根据@Vimy的回答,最终得到了输出

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
************

图像具有属性的所有键值对...

但根据文档,必须有更多可用的属性

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

如何访问所有左侧属性?

1 个答案:

答案 0 :(得分:2)

这个辅助函数应该可以解决问题。

 func getEXIFFromImage(image:NSData) -> NSDictionary {

    let imageSourceRef = CGImageSourceCreateWithData(image, nil);

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

    let mutableDict = NSMutableDictionary(dictionary: currentProperties!)

    return mutableDict

}

然后,您可以使用CGImageProperties中的键来访问所需的元数据。 https://developer.apple.com/reference/imageio/cgimageproperties/exif_dictionary_keys