我正在构建一个应用程序,允许用户在本地或通过网络选择文件和/或文件夹,并在一些过滤后在NSTableView中列出该选择的内容(没有隐藏文件,只接受.tif,.eps )。然后,用户可以从列表中选择文件名,然后显示文件元数据。至少这就是我想要发生的事情。现在我为元数据返回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/Illustration%20Pickup/Archive/AL013111_IL_Communication.eps
答案 0 :(得分:-1)
我无法分辨文件网址中的“localhost”部分来自哪里,但我认为这是罪魁祸首。文件URL通常不包含“localhost”部分。在您的示例中,它应如下所示:
file:///Volumes/STORAGE%20SVR/Illustration-Wofford/Illustration%20Pickup/Archive/AL013111_IL_Communication.eps
但我很确定你现在已经弄清楚了这一点:)
更新:Mike的评论让我更正了:file://localhost/...
与file:///...
的内容相同!