简体   繁体   English

在Swift,Cocoa,Mac OSX中将EXIF数据设置为NSImage

[英]Set EXIF data to a NSImage in Swift, Cocoa, Mac OSX

I am trying to set EXIF data to a NSImage if the image doesn't have EXIF data. 如果图像没有EXIF数据,我试图将EXIF数据设置为NSImage。 Actually I want just set the DateTimeOriginal. 实际上我只想设置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'. 如果图像具有exif数据,我只是从中获取DateTimeOriginal并将其存储在名为“ dateString”的字符串变量中。 But if it doesn't have exif data (in the 'else' case) I'm creating a dictionary where I store the Date. 但是,如果它没有exif数据(在“ else”情况下),我将创建一个字典来存储Date。 Then I'm adding the exif data to an NSBitmapImageRep. 然后,我将exif数据添加到NSBitmapImageRep。 And finally I want to add this imagerep to the image. 最后,我想将此imagerep添加到图像中。 I guess the last step is the problem. 我想最后一步是问题。

Can anybody help me out with my problem? 有人可以帮我解决我的问题吗?

Thanks! 谢谢!

//You can use CGImageSourceRef instead of NSBitMapImageRef. //您可以使用CGImageSourceRef代替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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM