tags:

views:

200

answers:

2

I've Declared a string Like so

NSString* fileName = [files objectAtIndex:i]; NSLog(fileName);

NSImage* imageFromBundle = [[NSImage alloc] initWithContentsOfFile:fileName];

and want to use that filename to open a file in a different directory. I came up with this

NSImage* imageFromBundle2; imageFromBundle2 = [[NSImage alloc] initWithContentsOfFile:@"/Users/rhaynes/Documents/works4/" filename ];

Any help would be appreciated

A: 

Hey Casino,

I'll assume that your fileName string is actually a file name, like "myImage.png". A lot of the Objective-C docs refer to a file name when they really mean file path - so sometimes it's confusing.

What you want to do is create an NSString that represents the complete path to the file you want to load. For instance, you could say:

NSString * path = [NSString stringWithFormat: @"/Users/rhaynes/Documents/works4/%@", fileName];

That line creates a new NSString using the format string and parameters provided (the %@ in the format string indicates that the string value of fileName should be inserted there.) StringWithFormat is a really powerful function, so you should definitely check it out in the docs.

Then you could call initWithContentsOfFile:path, and it should give you the image you want.

Ben Gotow
WoW U are the MAN. Thanks for the Quick Reply, You have gotten me closer to my answer, however my log file shows the complete path to my file instead of just the fiename2009-06-06 23:22:40.188 appagain2[26864] Users/rhaynes/Documents/works4//Users/rhaynes/Documents/works3/work1.jpgDo you know of a way to get the filename only likeUsers/rhaynes/Documents/works4/work1.jpg
Ok. So that means that your fileName variable is actually a full path. What you really want is just the last part: "work1.jpg". Then you can append that to the "right" path. The Objective-C string class provides a neat function called lastPathComponent that will return just that part.So in the stringWithFormat: line I put in my answer, just replace fileName with [fileName lastPathComponent]. That should do it!
Ben Gotow
THANK YOU SO MUCH... That did the trick.
A: 

NSString* fileName = [files objectAtIndex:i]; NSLog(fileName);

Don't pass non-hard-coded strings as format-string arguments. If they contain format specifiers, you'll get garbage or a crash. (Try this with fileName = @"foo%sbar", for example. Then try it with fileName = @"foo%fbar" for even more fun.)

Your NSLog statement should be:

NSLog(@"%@", fileName);

[I] want to use that filename to open a file in a different directory. I came up with this

NSImage* imageFromBundle2; imageFromBundle2 = [[NSImage alloc] initWithContentsOfFile:@"/Users/rhaynes/Documents/works4/" filename ];

You can only concatenate string literals this way; as you've no doubt seen for yourself, this is a syntax error when one of the strings isn't a literal.

First off, if fileName is actually a pathname, you'll need to use lastPathComponent to get the actual filename. So:

NSString *path = [files objectAtIndex:i];
NSString *filename = [path lastPathComponent];

Then, use stringByAppendingPathComponent: to tack this onto the new superpath.

NSString *desiredFilenamePath = [directoryPath stringByAppendingPathComponent:filename];

Now you have the pathname you wanted to pass to NSImage's initializer.

Peter Hosey