Reputation: 863
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!
Upvotes: 3
Views: 1309
Reputation: 529
//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
Upvotes: 1