简体   繁体   中英

Cocoa: Getting image file metadata

I am building an app that allows users to select a file and/or folder either locally or across the network and list the contents of that selection in a NSTableView after some filtering (no hidden files, only accepting .tif, .eps). The user can then select a file name from the list and then have the files metadata shown to them. At least that is what I want to happen. Right now I am getting null returned for the metadata. Here's my code:

- (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];

}

I'm guessing the problem here is the CFURLREF in CGImageSourceCreateWithURL. Instead of NSURL should I be using something else?

Thanks

Here's the path I am passing (logged from filePathURL): file://localhost/Volumes/STORAGE%20SVR/Illustration-Wofford/Illustration%20Pickup/Archive/AL013111_IL_Communication.eps

I can't tell where the "localhost" part in your file URL comes from, but I think that's the culprit. A file url doesn't usually contain a "localhost" part. In your example, it should look like this:

file:///Volumes/STORAGE%20SVR/Illustration-Wofford/Illustration%20Pickup/Archive/AL013111_IL_Communication.eps

But I'm pretty sure you've figured this out by now :)

Update : I stand corrected by Mike's comment: file://localhost/... is the same thing as file:///... !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM