Cocoa:获取图像文件元数据

Cocoa:获取图像文件元数据,cocoa,metadata,nsurl,Cocoa,Metadata,Nsurl,我正在开发一个应用程序,允许用户在本地或网络上选择一个文件和/或文件夹,并在经过筛选后(无隐藏文件,仅接受.tif、.eps)在NSTableView中列出该选择的内容。然后,用户可以从列表中选择一个文件名,然后向他们显示文件元数据。至少这是我想要的。现在,元数据返回null。这是我的密码: - (void)tableViewSelectionDidChange:(NSNotification *)notif { NSDictionary* metadata = [[NSDictionary

我正在开发一个应用程序,允许用户在本地或网络上选择一个文件和/或文件夹,并在经过筛选后(无隐藏文件,仅接受.tif、.eps)在NSTableView中列出该选择的内容。然后,用户可以从列表中选择一个文件名,然后向他们显示文件元数据。至少这是我想要的。现在,元数据返回null。这是我的密码:

- (void)tableViewSelectionDidChange:(NSNotification *)notif {

NSDictionary* metadata = [[NSDictionary alloc] init];

//get selected item
NSString* rowData = [fileList objectAtIndex:[tblFileList selectedRow]];

//set path to file selected
NSString* filePath = [NSString stringWithFormat:@"%@/%@", objPath, rowData];

//declare a file manager
NSFileManager* fileManager = [[NSFileManager alloc] init];

//check to see if the file exists
if ([fileManager fileExistsAtPath:filePath] == YES) {

    //escape all the garbage in the string
    NSString *percentEscapedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)filePath, NULL, NULL, kCFStringEncodingUTF8);

    //convert path to NSURL
    NSURL* filePathURL = [[NSURL alloc] initFileURLWithPath:percentEscapedString];

    NSError* error;
    NSLog(@"%@", [filePathURL checkResourceIsReachableAndReturnError:error]);
        //declare a cg source reference
        CGImageSourceRef  sourceRef;

        //set the cg source references to the image by passign its url path
        sourceRef = CGImageSourceCreateWithURL((CFURLRef)filePathURL, NULL);

        //set a dictionary with the image metadata from the source reference
        metadata = (NSDictionary *)CGImageSourceCopyPropertiesAtIndex(sourceRef,0,NULL);

        NSLog(@"%@", metadata);

        [filePathURL release];


} else {

    [self showAlert:@"I cannot find this file."];
}

[fileManager release];

}
我猜这里的问题是CGImageSourceCreateWithURL中的CFURLREF。我应该用别的东西来代替NSURL吗

谢谢

以下是我要传递的路径(从filePathURL记录):file://localhost/Volumes/STORAGE%20SVR/Illustration-Wofford/插图%20Pickup/Archive/AL013111_IL_Communication.eps

我不知道文件URL中的“localhost”部分来自何处,但我认为这是罪魁祸首。文件url通常不包含“localhost”部分。在您的示例中,它应该如下所示:

file:///Volumes/STORAGE%20SVR/Illustration-Wofford/Illustration%20Pickup/Archive/AL013111_IL_Communication.eps
但我敢肯定你现在已经明白了:)


更新:迈克的评论纠正了我的错误:
file://localhost/...
file:///...

这篇文章建议您正确使用NSURL/CFURLRef:。您确定objPath是正确的,因为它已经成功地生成了文件名吗?如果没有,也许你应该测试url是否正确。我添加了一个文件管理器检查,以确保文件在filePath中存在,并且我记录了sourceRef,然后返回。可能是索引。您可以尝试CGImageSourceCopyProperties(sourceRef,NULL);一样。当我把它添加到代码中时:NSError*error;NSLog(@“%@,[filePathURL checkResourceIsReachableAndReturnError:error]);我得到一个警告,NSURL可能不会响应checkResource…我认为错误在于URL格式不正确或类似的东西…我还将此方法添加到代码中-CFURLCreateStringByAddingPercentEscapes以清除任何空格和其他内容,但仍然轰炸…您对
CFURLCreateStringByAddingPercentEscapes
没有任何意义。
-initFileURLWithPath:
和friends的全部要点是,它们为您执行的任何转义都是完全错误的。文件URL指定localhost作为主机名,或者指定第三个斜杠表示相同的内容。这两种方法都同样有效,框架可以很好地处理它们。谢谢你提供的信息,Mike!我不知道:)