简体   繁体   English

使用 EXIF 数据进行 Swift base64 图像编码

[英]Swift base64 image encoding with EXIF data

I am currently using base64 encoding to convert and sent multiple images in a JSON file from my Swift app to my API using:我目前正在使用 base64 编码将 JSON 文件中的多个图像从我的 Swift 应用程序转换并发送到我的 API,使用:

let imageData = image.jpegData(compressionQuality: 1.0)
let sSideL = imageData.base64EncodedString(options: .lineLength64Characters)

While extending my API, I now would like to use the rich EXIF data provided by most smartphones like lense information, field of view or the device model .在扩展我的 API 的同时,我现在想使用大多数智能手机提供的丰富 EXIF 数据,如镜头信息、视野或设备模型 Most important for my current purpose is the "Image Model" tag, in order to identify the device, which took the picture.对我目前的目的来说最重要的是"Image Model"标签,以便识别拍摄照片的设备。

I recognized that there are some EXIF data left in the base64 data coming through my API but it is limited to the orientation and very basic information like the orientation.我认识到在通过我的 API base64数据中还有一些 EXIF 数据,但它仅限于方向和非常基本的信息,如方向。 Also when I directly print the base64String in Xcode and analyze it, it has very poor EXIF information.此外,当我直接在 Xcode 中打印 base64String 并对其进行分析时,它的 EXIF 信息非常差。 Technically it should be possible, because converting the same image in an online base64 converter and analyzing the returning string, I am able to see EXIF information like "Image Model", etc.从技术上讲应该是可能的,因为在在线 base64 转换器中转换相同的图像并分析返回的字符串,我能够看到 EXIF 信息,如“图像模型”等。

Is there a way to convert my UIImage to a base64 string keeping all EXIF details?有没有办法将我的 UIImage 转换为保留所有 EXIF 详细信息的 base64 字符串?

The API represents the main part of my system, so I would like to keep it as simple as possible and not add additional upload parameter. API 代表我系统的主要部分,所以我希望它尽可能简单并且不添加额外的上传参数。

EDIT Here my code to capture the UIImage编辑这里我的代码来捕获 UIImage

extension CameraController: AVCapturePhotoCaptureDelegate {


    public func photoOutput(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhoto photoSampleBuffer: CMSampleBuffer?, previewPhoto previewPhotoSampleBuffer: CMSampleBuffer?,
                        resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Swift.Error?) {

        if let error = error {
            // ERROR
        }
        else if let buffer = photoSampleBuffer,
            let data = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: buffer, previewPhotoSampleBuffer: nil),
            let image = UIImage(data: data) {

            // SEND IMAGE TO SERVER
        }
        else {
            // UNKNOWN ERROR
        }
    }
}

You can use the newer (iOS 11+) delegate method:您可以使用较新的(iOS 11+)委托方法:

    public func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
            if let error = error {
        // ERROR
            } else if let data = photo.fileDataRepresentation() {

        // SEND IMAGE DATA TO SERVER
    }
    else {
        // UNKNOWN ERROR
    }
}

or the method you are using:或您使用的方法:

    public func photoOutput(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhoto photoSampleBuffer: CMSampleBuffer?, previewPhoto previewPhotoSampleBuffer: CMSampleBuffer?,
                    resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Swift.Error?) {

    if let error = error {
        // ERROR
    } else if let buffer = photoSampleBuffer,
        let data = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: buffer, previewPhotoSampleBuffer: nil) {
        // SEND IMAGE DATA TO SERVER
    }
    else {
        // UNKNOWN ERROR
    }
}

Like leo-dabus mentioned, you need to send the image data to the server, that has the metadata in it.就像提到的 leo-dabus 一样,您需要将图像数据发送到服务器,其中包含元数据。 If you first create an UIImage and convert that back again to data, you have lost the metadata.如果您首先创建一个 UIImage 并将其再次转换回数据,那么您已经丢失了元数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM