gpt4 book ai didi

macos - 在 Swift、Cocoa、Mac OSX 中将 EXIF 数据设置为 NSImage

转载 作者:搜寻专家 更新时间:2023-11-01 05:40:06 31 4
gpt4 key购买 nike

如果图像没有 EXIF 数据,我试图将 EXIF 数据设置为 NSImage。实际上我只想设置 DateTimeOriginal。但我的方法行不通。

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
}
}

如果图像有 exif 数据,我只需从中获取 DateTimeOriginal 并将其存储在名为“dateString”的字符串变量中。但是如果它没有 exif 数据(在“其他”情况下),我正在创建一个字典来存储日期。然后我将 exif 数据添加到 NSBitmapImageRep。最后我想将这个 imagerep 添加到图像中。我猜最后一步是问题所在。

谁能帮我解决我的问题?

谢谢!

最佳答案

//你可以使用CGImageSourceRef代替NSBitMapImageRef。对于初学者:

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

关于macos - 在 Swift、Cocoa、Mac OSX 中将 EXIF 数据设置为 NSImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31706502/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com