Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write Exif data to image in Swift with lat long

Tags:

I am trying to write EXIF data to the image but CGImageDestinationFinalize crashes:

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)
like image 972
Inderpal Singh Avatar asked Mar 21 '18 09:03

Inderpal Singh


People also ask

Does EXIF data include GPS?

Exchangeable Image File Format (EXIF) is a standard that defines specific information related to an image or other media captured by a digital camera. It is capable of storing such important data as camera exposure, date/time the image was captured, and even GPS location.

How do I put EXIF on a photo?

You need first to copy the exif files located here google exif to your project, then use the following code : ExifInterface exif = new ExifInterface(); exif.

How do you write an EXIF?

To write Exif data to a JPEG, you need to write an APP1/Exif segment as part of the normal JIF structure. The EXIFWriter will write the data you should put inside this segment only. Everything else must be provided by you.

How do you enter EXIF data?

On a Windows PC using File Explorer right-click on the file you want to see the data for. You will see a window pop up with various options. Click on Properties and then on Details. This will bring up the EXIF data for that photo.


2 Answers

Please check this below answer. you got error due to nil value on EXIFDictionary and GPSDictionary

 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)
like image 188
iOS Avatar answered Nov 11 '22 04:11

iOS


This could be from your destination definition.

This worked for me

(...)
    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
}
like image 33
Jan ATAC Avatar answered Nov 11 '22 04:11

Jan ATAC