0

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

PruitIgoe
  • 6,166
  • 16
  • 70
  • 137
  • This post suggests you are correct to use NSURL/CFURLRef: [link](http://stackoverflow.com/questions/3410887/cfurlcreatedataandpropertiesfromresource-failed-with-error-code-15). Are you sure the objPath is correct because it's already successfully producing the file name? If not, maybe you should test that the url is correct. – Wienke Mar 23 '11 at 15:17
  • I added a file manager check to make sure the file exists at filePath, and I logged the sourceRef and that came back with . – PruitIgoe Mar 23 '11 at 15:39
  • Maybe it's the index. You could try CGImageSourceCopyProperties(sourceRef, NULL); – Wienke Mar 23 '11 at 15:51
  • Same. When I added this to the code: NSError* error; NSLog(@"%@", [filePathURL checkResourceIsReachableAndReturnError:error]); I get a warning that NSURL might not respond to checkResource...I'm thinking the error is that the URL is not formatted properly or something like that...I also added this method to the code - CFURLCreateStringByAddingPercentEscapes to clear out any spaces and other stuff but still bombing... – PruitIgoe Mar 23 '11 at 16:39
  • Your use of `CFURLCreateStringByAddingPercentEscapes` doesn't make any sense. The whole point of `-initFileURLWithPath:` and friends is that they perform any escaping for you – Mike Abdullah May 17 '12 at 10:20

1 Answers1

-1

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:///...!

Johannes Fahrenkrug
  • 42,912
  • 19
  • 126
  • 165
  • 1
    Flat out wrong. File urls specify either localhost as the host name, or a third slash to indicate the same thing. Both are equally valid and the frameworks handle them just fine – Mike Abdullah May 16 '12 at 19:51