Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos 将EXIF数据设置为Swift、Cocoa、Mac OSX中的NSImage_Macos_Swift_Cocoa_Exif_Nsimage - Fatal编程技术网

Macos 将EXIF数据设置为Swift、Cocoa、Mac OSX中的NSImage

Macos 将EXIF数据设置为Swift、Cocoa、Mac OSX中的NSImage,macos,swift,cocoa,exif,nsimage,Macos,Swift,Cocoa,Exif,Nsimage,如果图像没有EXIF数据,我尝试将EXIF数据设置为NSImage。事实上,我只想把DateTimeOriginal设置好。但我的方法行不通 let completePath = "/Users/stefocdp/image.jpg" let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "dd.MM.yyyy HH:mm:ss" let folderAttributes: NSDictionary = NSFileMa

如果图像没有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
    }
}
let completePath=“/Users/stefocdp/image.jpg”
让dateFormatter=NSDateFormatter()
dateFormatter.dateFormat=“dd.MM.yyyy HH:MM:ss”
让FolderAttribute:NSDictionary=NSFileManager.defaultManager().AttributesFiteMatPath(completePath,错误:nil)!
var dateString:String=dateFormatter.stringFromDate(folderAttributes.objectForKey(NSFileCreationDate)为!NSDate)
对于self.image.representations中的obj{
如果let rep=obj as?NSBitmapImageRep{
如果rep.valueForProperty(NSImageEXIFData)!=nil{
//如果图像具有exif数据,则将调用
//发出命令
var exifData=rep.valueForProperty(NSImageEXIFData)as!NSDictionary
//将CFString转换为字符串
var cfStr:CFString=kCGImagePropertyExifDateTimeOriginal
var nsTypeString=cfStr作为NSString
var keySwiftString:String=nsTypeString作为字符串
//从exif数据获取日期
dateString=exifData.valueForKey(KeyswitString)作为!字符串
//将日期转换为NSDate
var dateFormatter=NSDateFormatter()
dateFormatter.dateFormat=“yyyy:MM:dd HH:MM:ss”
var date=dateFormatter.dateFromString(dateString)
//将NSDate转换回字符串(仅因为我需要另一种日期格式)
dateFormatter.dateFormat=“dd.MM.yyyy HH:MM:ss”
dateString=dateFormatter.stringFromDate(日期!)
}否则{
//如果映像没有exif数据,将调用
//为exif数据创建字典
var exifData=Dictionary()
//将给定的日期字符串转换为NSDate
var dateFormatter=NSDateFormatter()
dateFormatter.dateFormat=“dd.MM.yyyy HH:MM:ss”
var date=dateFormatter.dateFromString(dateString)
//将其转换回字符串(exif数据的不同日期格式)
dateFormatter.dateFormat=“yyyy:MM:dd HH:MM:ss”
dateString=dateFormatter.stringFromDate(日期!)
//将CFString转换为字符串
var cfStr:CFString=kCGImagePropertyExifDateTimeOriginal
var nsTypeString=cfStr作为NSString
var keySwiftString:String=nsTypeString作为字符串
//将日期添加到exifData字典
exifData[KeyswitString]=日期字符串作为CFString
//将exifData设置为NSBitmapImageRep
rep.setProperty(NSImageEXIFData,带值:exifData)
//将NSBitmapImageRep添加到映像
self.image.addRepresentation(rep)
}
}
否则{
//不是NSBitmapImageRep的实例
}
}
如果图像有exif数据,我只需从中获取DateTimeOriginal并将其存储在名为“dateString”的字符串变量中。但是如果它没有exif数据(在“else”的情况下),我将创建一个字典来存储日期。然后我将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

你可能想看看有趣的东西。但设置EXIF值的问题不就是日期吗?您的答案只得到值。这和OP的代码有什么关系?我很好奇。我发现NSBitMapImageRep不适合我,这可能是错误。从NSBitMapImageRep和CGImageSourceRef创建字典,然后比较每个的打印(dic.description),显示格式非常不同的输出。