我正在構建一個應用程序,允許用戶在本地或網絡上選擇文件和/或文件夾,並在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記錄):文件://localhost/Volumes/STORAGE%20SVR/Illustration-Wofford/Illustration%20Pickup/Archive/AL013111_IL_Communication.eps
這篇文章建議您正確使用NSURL/CFURLRef:[link](http://stackoverflow.com/questions/3410887/cfurlcreatedataandpropertiesfromresource-failed-with-error-code-15)。你確定objPath是正確的,因爲它已經成功生成文件名?如果沒有,也許你應該測試該網址是否正確。 – Wienke 2011-03-23 15:17:08
我添加了一個文件管理器檢查,以確保該文件存在於filePath中,並且我記錄了sourceRef並返回了。 –
PruitIgoe
2011-03-23 15:39:46
也許這是索引。你可以嘗試CGImageSourceCopyProperties(sourceRef,NULL); – Wienke 2011-03-23 15:51:23