Ios 如何使用lat long将Exif数据写入Swift中的图像

Ios 如何使用lat long将Exif数据写入Swift中的图像,ios,swift,exif,Ios,Swift,Exif,我试图将EXIF数据写入图像,但CGImageDestinationFinalize崩溃: var image = info[UIImagePickerControllerOriginalImage] as! UIImage let jpeg = UIImageJPEGRepresentation(image, 1.0) var source: CGImageSource? = nil source = CGImageSourceCreateWithData((jpeg

我试图将EXIF数据写入图像,但CGImageDestinationFinalize崩溃:

var image = info[UIImagePickerControllerOriginalImage] as! UIImage
    let jpeg = UIImageJPEGRepresentation(image, 1.0)
    var source: CGImageSource? = nil
    source = CGImageSourceCreateWithData((jpeg 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]

    GPSDictionary![(kCGImagePropertyGPSLatitude as String)] = 30.21313
    GPSDictionary![(kCGImagePropertyGPSLongitude as String)] = 76.22346
    EXIFDictionary![(kCGImagePropertyExifUserComment as String)] = "Hello Image"


let UTI: CFString = CGImageSourceGetType(source!)!
    let dest_data = NSMutableData()
    let destination: CGImageDestination = CGImageDestinationCreateWithData(dest_data as CFMutableData, UTI, 1, nil)!
    CGImageDestinationAddImageFromSource(destination, source!, 0, (metadataAsMutable as CFDictionary?))
    CGImageDestinationFinalize(destination)

这可能来自您的目的地定义

这对我有用

(...)
    let source                  =   CGImageSourceCreateWithData(jpgData as CFData, nil)

    let finalData               =   NSMutableData()

    let destination             =   getDestination(finalData:finalData, source:source!)

(...)

// Note that :
// NSMutableData type variable will be cast to CFMutableData
// 
fileprivate func getDestination(finalData:CFMutableData, source:CGImageSource)->CGImageDestination?{
    guard let destination = CGImageDestinationCreateWithData(finalData,
                                                             CGImageSourceGetType(source)!,
                                                             1,
                                                             nil)else{return nil}
    return destination
}

请检查下面的答案。 由于EXIFDictionarygpsddictionary

 var image = info[UIImagePickerControllerOriginalImage] as! UIImage
 let jpeg = UIImageJPEGRepresentation(image, 1.0)
 var source: CGImageSource? = nil
 source = CGImageSourceCreateWithData((jpeg 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
   EXIFDictionary![(kCGImagePropertyExifUserComment as String)] = "Hello Image"

   let UTI: CFString = CGImageSourceGetType(source!)!
   let dest_data = NSMutableData()
   let destination: CGImageDestination = CGImageDestinationCreateWithData(dest_data as CFMutableData, UTI, 1, nil)!
   CGImageDestinationAddImageFromSource(destination, source!, 0, (metadataAsMutable as CFDictionary?))
            CGImageDestinationFinalize(destination)

请显示有关崩溃的更多信息。请显示崩溃日志!这段代码对我有用。@Lenin如何将图像(带有exif数据)保存到文件中?@niczm25检查此项让documentsDirectoryURL=try!FileManager().url(对于:.documentDirectory,在:.userDomainMask中,适用于:nil,create:true)让fileURL=documentsDirectoryURL.appendingPathComponent(“文件名”),如果!FileManager.default.fileExists(atPath:fileURL.path){do{try dest_data.write(to:fileURL)}catch{print(error.localizedDescription)}目标C的else{}?