Comment écrire des données Exif à L'image dans Swift

je suis en train d'écrire les données EXIF de l'image, mais CGImageDestinationFinalize se bloque:

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)
17
demandé sur Matt Stillwell 2018-03-21 12:40:50

2 réponses

s'il vous Plaît vérifier ci-dessous la réponse. vous avez l'erreur due à néant valeur sur Exifdictionnaire et 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)
9
répondu iOS 2018-03-27 06:14:14

cela peut provenir de votre définition de destination.

Cela a fonctionné pour moi

(...)
    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
}
1
répondu Jan ATAC 2018-03-24 07:50:07