gpt4 book ai didi

swift - 在 Swift 中将 GPS 元数据写入 EXIF

转载 作者:行者123 更新时间:2023-11-28 13:50:37 50 4
gpt4 key购买 nike

我正在尝试将 gps 坐标写入在我的应用程序中拍摄的图像。所以我四处寻找要使用的代码,然后我编写了一个函数,它接受 Data 并返回 Data(因为我需要像这样将它发送到 Firebase.

但无论我如何编写此代码,元数据都不会“粘附”到图片上。图片在上传时看起来是正确的,但在下载时它包含的元数据不超过照片的大小。

我是不是完全错过了什么?

func setMetaData(imageData: Data) -> Data? {

var source: CGImageSource? = nil
source = CGImageSourceCreateWithData((imageData as CFData?)!, nil)
let metadata = CGImageSourceCopyPropertiesAtIndex(source!, 0, nil) as? [AnyHashable: Any]
var metadataAsMutable = metadata
var EXIFDictionary = (metadataAsMutable?[(kCGImagePropertyExifDictionary as String)]) as? [AnyHashable: Any]
var GPSDictionary = (metadataAsMutable?[(kCGImagePropertyGPSDictionary as String)]) as? [AnyHashable: Any]

if !(EXIFDictionary != nil) {
EXIFDictionary = [AnyHashable: Any]()
}
if !(GPSDictionary != nil) {
GPSDictionary = [AnyHashable: Any]()
}

GPSDictionary![(kCGImagePropertyGPSLatitude as String)] = 30.21313
GPSDictionary![(kCGImagePropertyGPSLongitude as String)] = 76.22346

let dest_data = NSMutableData()
guard let imageDest = CGImageDestinationCreateWithData(dest_data as CFMutableData, kUTTypeJPEG, 1, nil) else { return nil }
CGImageDestinationAddImageFromSource(imageDest, source!, 0, (metadataAsMutable as CFDictionary?))


if CGImageDestinationFinalize(imageDest) {

return dest_data as Data
} else {
print("FAILED")
}
return nil
}

最佳答案

我找到了一个不太漂亮的解决方案,但它完全可以完成工作

func addMetaData(data: Data) -> NSData? {

guard let source = CGImageSourceCreateWithData(data as CFData, nil) else {return nil}
guard let type = CGImageSourceGetType(source) else {return nil}
let mutableData = NSMutableData(data: data)
guard let destination = CGImageDestinationCreateWithData(mutableData, type, 1, nil) else {
return nil}

guard let path = Bundle.main.url(forResource: "predict_pic", withExtension: "jpg") else {
return nil }
let imageSource = CGImageSourceCreateWithURL(path as CFURL, nil)
let imageProperties = CGImageSourceCopyMetadataAtIndex(imageSource!, 0, nil)

let mutableMetadata = CGImageMetadataCreateMutableCopy(imageProperties!)

CGImageMetadataSetValueMatchingImageProperty(mutableMetadata!, kCGImagePropertyGPSDictionary, kCGImagePropertyGPSLatitudeRef, "N" as CFTypeRef)
CGImageMetadataSetValueMatchingImageProperty(mutableMetadata!, kCGImagePropertyGPSDictionary, kCGImagePropertyGPSLatitude, location.coordinate.latitude as CFTypeRef)
CGImageMetadataSetValueMatchingImageProperty(mutableMetadata!, kCGImagePropertyGPSDictionary, kCGImagePropertyGPSLongitudeRef, "E" as CFTypeRef)
CGImageMetadataSetValueMatchingImageProperty(mutableMetadata!, kCGImagePropertyGPSDictionary, kCGImagePropertyGPSLongitude, location.coordinate.longitude as CFTypeRef)

let finalMetadata:CGImageMetadata = mutableMetadata!
CGImageDestinationAddImageAndMetadata(destination, UIImage(data: data)!.cgImage! , finalMetadata, nil)
guard CGImageDestinationFinalize(destination) else { return nil }
return mutableData;
}

关于swift - 在 Swift 中将 GPS 元数据写入 EXIF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54678780/

50 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com