3

I am trying to set EXIF data to a NSImage if the image doesn't have EXIF data. Actually I want just set the DateTimeOriginal. But my approach doesn't work.

let completePath = "/Users/stefocdp/image.jpg"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd.MM.yyyy HH:mm:ss"
let folderAttributes: NSDictionary = NSFileManager.defaultManager().attributesOfItemAtPath(completePath, error: nil)!
var dateString: String = dateFormatter.stringFromDate(folderAttributes.objectForKey(NSFileCreationDate) as! NSDate)

for obj in self.image.representations {
    if let rep = obj as? NSBitmapImageRep {
        if rep.valueForProperty(NSImageEXIFData) != nil {
            //will be called if the image has exif data

            // get exif dictonary
            var exifData = rep.valueForProperty(NSImageEXIFData) as! NSDictionary

            //convert CFString to String
            var cfStr:CFString = kCGImagePropertyExifDateTimeOriginal
            var nsTypeString = cfStr as NSString
            var keySwiftString:String = nsTypeString as String

            //get date from exif data
            dateString = exifData.valueForKey(keySwiftString) as! String

            //convert the date to NSDate
            var dateFormatter = NSDateFormatter()
            dateFormatter.dateFormat = "yyyy:MM:dd HH:mm:ss"
            var date = dateFormatter.dateFromString(dateString)

            //convert the NSDate back to String (only because I want another date format)
            dateFormatter.dateFormat = "dd.MM.yyyy HH:mm:ss"
            dateString = dateFormatter.stringFromDate(date!)
        } else {
            //will be called if the image don't has exif data

            //create dictionary for the exif data
            var exifData = Dictionary<String, CFString>()

            //convert the given date string to NSDate
            var dateFormatter = NSDateFormatter()
            dateFormatter.dateFormat = "dd.MM.yyyy HH:mm:ss"
            var date = dateFormatter.dateFromString(dateString)

            //convert it back to String (different date format for exif data)
            dateFormatter.dateFormat = "yyyy:MM:dd HH:mm:ss"
            dateString = dateFormatter.stringFromDate(date!)

            //convert CFString to String
            var cfStr:CFString = kCGImagePropertyExifDateTimeOriginal
            var nsTypeString = cfStr as NSString
            var keySwiftString:String = nsTypeString as String

            //add date to the exifData dictionary
            exifData[keySwiftString] = dateString as CFString

            //set the exifData to the NSBitmapImageRep
            rep.setProperty(NSImageEXIFData, withValue: exifData)

            //add the NSBitmapImageRep to the image
            self.image.addRepresentation(rep)
        }
    }
    else {
        //is not an instance of NSBitmapImageRep
    }
}

If the image has exif data I just get the DateTimeOriginal out of it and store it in a string variable called 'dateString'. But if it doesn't have exif data (in the 'else' case) I'm creating a dictionary where I store the Date. Then I'm adding the exif data to an NSBitmapImageRep. And finally I want to add this imagerep to the image. I guess the last step is the problem.

Can anybody help me out with my problem?

Thanks!

stefOCDP
  • 803
  • 2
  • 12
  • 20
  • you might want to look over [here](http://stackoverflow.com/questions/5125323/problem-setting-exif-data-for-an-image) – StefanS Jul 30 '15 at 12:18

1 Answers1

1

//You can use CGImageSourceRef instead of NSBitMapImageRef. And for starters:

let url = NSURL(fileURLWithPath: imgfile) //imgfile = a String of the img file path

let cgiSrc = CGImageSourceCreateWithURL(url,nil)

let cfD:CFDictionaryRef = CGImageSourceCopyPropertiesAtIndex(cgiSrc!, 0, nil)!

let nsDic = NSDictionary(dictionary: cfD)

print(nsDic.description) //verify that it's working
PZX
  • 529
  • 1
  • 4
  • 7
  • Interesting. But isn't the question about *setting* an EXIF value like the date? Your answer only *gets* the values. How does it go with OP's code? I'm curious. – Eric Aya Jan 22 '16 at 15:07
  • I found that NSBitMapImageRep was just not working for me, and that might be the fault. – PZX Jan 23 '16 at 20:43
  • Creating the dictionary from NSBitMapImageRep and then from CGImageSourceRef, and then comparing the print(dic.description) for each, shows output that is very different in format. – PZX Jan 23 '16 at 21:37