Ios 如何用元数据丰富UIImage数据
在我的应用程序中,我试图让图像数据包含元数据(位置、时间戳等)。我正在使用UIImagePickerController进行图像捕获,其委托函数具有:Ios 如何用元数据丰富UIImage数据,ios,swift,uiimage,phasset,Ios,Swift,Uiimage,Phasset,在我的应用程序中,我试图让图像数据包含元数据(位置、时间戳等)。我正在使用UIImagePickerController进行图像捕获,其委托函数具有: info[UIImagePickerController.InfoKey.originalImage] info[UIImagePickerController.InfoKey.phAsset] info[UIImagePickerController.InfoKey.mediaMetadata] 因此,对于从库中拾取的图像,phAsset具
info[UIImagePickerController.InfoKey.originalImage]
info[UIImagePickerController.InfoKey.phAsset]
info[UIImagePickerController.InfoKey.mediaMetadata]
因此,对于从库中拾取的图像,phAsset
具有我所需的一切。我只是使用.getDataFromPHAsset
函数来获取丰富的数据。但是,对于刚刚拍摄的图像,。phAsset
为零。我曾考虑尝试以某种方式将.originalImage
和.mediaMetadata
组合到一个数据对象中,但无法获得所需的结果。我尝试使用这种方法:
我知道我也可以使用AVCaptureSession
定制imageCapture控制器,并在didFinishProcessingPhoto
委托调用中使用AVCapturePhoto
函数.fileDataRepresentation()
,但这不是我的第一选择
非常感谢您提供的任何帮助。我确信mediaMetadata不会提供位置信息。用这个 然后使用下面的函数将元数据添加到图像数据中:
func addImageProperties(imageData: Data, properties: NSMutableDictionary) -> Data? {
let dict = NSMutableDictionary()
dict[(kCGImagePropertyGPSDictionary as String)] = properties
if let source = CGImageSourceCreateWithData(imageData as CFData, nil) {
if let uti = CGImageSourceGetType(source) {
let destinationData = NSMutableData()
if let destination = CGImageDestinationCreateWithData(destinationData, uti, 1, nil) {
CGImageDestinationAddImageFromSource(destination, source, 0, dict as CFDictionary)
if CGImageDestinationFinalize(destination) == false {
return nil
}
return destinationData as Data
}
}
}
return nil
}
用法:
if let imageData = image.jpegData(compressionQuality: 1.0), let metadata = locationManager.location?.exifMetadata() {
if let newImageData = addImageProperties(imageData: imageData, properties: metadata) {
// newImageData now contains exif metadata
}
}