在Swift中将GPS元数据写入EXIF


岁爵
2025-02-21 03:33:17 (3天前)
  1. 我正在尝试将gps坐标写入我的应用程序中拍摄的图像。所以我四处寻找要使用的代码,我写了一个函数,它接受数据并返回数据(因为我需要发送它...

2 条回复
  1. 0# ⑩Ⅵ嵗D夨憶 | 2019-08-31 10-32



    我找到了一个不太漂亮的解决方案,但它完全胜任




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

    2. guard let source = CGImageSourceCreateWithData(data as CFData, nil) else {return nil}
    3. guard let type = CGImageSourceGetType(source) else {return nil}
    4. let mutableData = NSMutableData(data: data)
    5. guard let destination = CGImageDestinationCreateWithData(mutableData, type, 1, nil) else {
    6.     return nil}
    7. guard let path = Bundle.main.url(forResource: "predict_pic", withExtension: "jpg") else {
    8.     return nil }
    9. let imageSource = CGImageSourceCreateWithURL(path as CFURL, nil)
    10. let imageProperties = CGImageSourceCopyMetadataAtIndex(imageSource!, 0, nil)
    11. let mutableMetadata = CGImageMetadataCreateMutableCopy(imageProperties!)
    12. CGImageMetadataSetValueMatchingImageProperty(mutableMetadata!, kCGImagePropertyGPSDictionary, kCGImagePropertyGPSLatitudeRef, "N" as CFTypeRef)
    13. CGImageMetadataSetValueMatchingImageProperty(mutableMetadata!, kCGImagePropertyGPSDictionary, kCGImagePropertyGPSLatitude, location.coordinate.latitude as CFTypeRef)
    14. CGImageMetadataSetValueMatchingImageProperty(mutableMetadata!, kCGImagePropertyGPSDictionary, kCGImagePropertyGPSLongitudeRef, "E" as CFTypeRef)
    15. CGImageMetadataSetValueMatchingImageProperty(mutableMetadata!, kCGImagePropertyGPSDictionary, kCGImagePropertyGPSLongitude, location.coordinate.longitude as CFTypeRef)
    16. let finalMetadata:CGImageMetadata = mutableMetadata!
    17. CGImageDestinationAddImageAndMetadata(destination, UIImage(data: data)!.cgImage! , finalMetadata, nil)
    18. guard CGImageDestinationFinalize(destination) else { return nil }
    19. return mutableData;
    20. }

    21. </code>

登录 后才能参与评论