Cocoa question: NSURL and Core Graphics to get metadata...

I know this isn’t exactly the place to post this question but I’ve been banging my head all morning to no avail. I’m trying to get the metadata of an image file (tif or eps) but I keep getting null for the dictionary that should hold that data.

  • (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] == 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];
      
              //also getting a warning here that NSURL might not accept this method, but its in the docs. 
      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];

}

Any help would be appreciated.

Your problem is simple

you’re making an URL an passing it to a method who expects a file path like ‘/path/to/image.jpg’ You can simply do this two ways.

  1. if you want to keep the url you can change NSURL* filePathURL = [[NSURL alloc] initFileURLWithPath:percentEscapedString]; to NSURL* filePathURL = [[NSURL alloc] initWithString:percentEscapedString];

  2. if you prefer to use system path you need to delete the percentEscapeString and pass filePath directly to your filePathURL creation

Thanks DJ…

That brought some progress as I got these error messages:

Thu Mar 24 09:07:52 ssuranie.merion.com CocoaApp[85650] : CGImageSourceCreateWithURL failed with error code -15.
Thu Mar 24 09:07:52 ssuranie.merion.com CocoaApp[85650] : CGImageSourceCopyPropertiesAtIndex image source parameter is nil

Here’s the URL I am passing to it:

/Volumes/STORAGE%20SVR/Illustration-Wofford/Illustration%20Pickup/Archive/LTC010109_IL_refinance.eps

Sorry, my fault,

The problem remains because the string isn’t an url because it doesn’t contain the protocol. It takes more than escaping special characters only. What’s missing is the protocol of the url so what you need is file:///Volumes/STORAGE%20SVR/Illustration-Wofford/Illustration%20Pickup/Archive/LTC010109_IL_refinance.eps (the three slashes after file: is correct).

Second post but I advise you to do the other way around.

use:

NSURL *filePathURL = [NSURL fileURLWithPath:@“/Volumes/STORAGE SVR/Illustration-Wofford/Illustration Pickup/Archive/LTC010109_IL_refinance.eps” isDirectory:NO];