Re: Error initializing an NSImage
Re: Error initializing an NSImage
- Subject: Re: Error initializing an NSImage
- From: Hisaoki Nishida <email@hidden>
- Date: Sat, 1 Mar 2003 16:16:42 -0500
Hi, thanks for your suggestions.
I used your much simpler code, and the initialization still fails.
image turns out to be nil again.
I tried this with a different project, trying to do the same simple
thing, and it still fails!
As you pointed out, filePath was nil, but using the simpler code didn't
help the result. :(
newFile is simply "test", name of my TIFF image file without the
extension.
And it's case sensitive.
Also, I did this:
image = [NSImage imageNamed:@"test"];
[image retain];
if(image) NSLog(@"image is ok");
if(!image) NSLog(@"image is nil");
[image release];
[image release] doesn't raise any errors...
Isn't sending a release message to nil an error?
What could be going on.
Thanks,
-yuki
On Saturday, March 1, 2003, at 06:11 AM, Prachi Gauriar wrote:
>
>
On Saturday, March 1, 2003, at 04:42 AM, Hisaoki Nishida wrote:
>
>
> I am trying to initialize an image, that's all I'm doing.
>
> But for some reason it fails, my NSAssert saying that filePath is nil.
>
> The bundle printed in NSLog as "NSBundle
>
> </Users/hisaokin/Desktop/MyApp/build/MyApp.app> (loaded)"
>
> newFile is valid. It's the name of the file without the extension, and
>
> it exists in a folder called Resources in my project folder. The
>
> extension is ".tiff". I tried with many TIFF images, same result. How
>
> could the filePath be nil? it's really puzzling me.
>
>
>
> Here's the code:
>
>
>
> NSBundle * mainBundle = [NSBundle mainBundle];
>
> NSString * filePath = [mainBundle pathForResource:newFile
>
> ofType:@"tiff"];
>
> NSImage * image = [[[NSImage alloc]
>
> initWithContentsOfFile:[filePath stringByExpandingTildeInPath]]
>
> autorelease];
>
>
>
>
You could simplify this whole process by doing this:
>
NSImage *image = [[NSImage imageNamed:newFile] retain];
>
>
Here's what the NSImage docs say about this:
>
>
+ (id)imageNamed:(NSString *)name
>
Returns the NSImage instance associated with name. The returned object
>
will be one that's been assigned a name with the setName: method.
>
>
If there's no known NSImage with name, this method tries to create one
>
by searching for image data in the application's main bundle (see
>
NSBundle's class description for a description of how the bundle's
>
contents are searched). If a file contains data for more than one
>
image, a separate representation is created for each. If an image
>
representation can't be found for name, no object is created, and nil
>
is returned.
>
>
The preferred way to name an image is to ask for a name without the
>
extension, but to include the extension for a filename.
>
>
The image returned by this method should not be freed, unless it's
>
certain that no other objects reference it.
>
>
If you want to use the method you're currently using, what does
>
NSLog(filePath) give you? Chances are that's probably nil. Make sure
>
your strings are all case-sensitive, as I suspect that Cocoa API calls
>
for file names are case-sensitive, even if HFS+ is not itself.
>
>
>
Prachi Gauriar
>
>
Computer Engineering Major/Mathematics Minor
>
University of Arkansas
>
Email: email@hidden
>
Web Site: http://comp.uark.edu/~pgauria/
>
Public Key: http://homepage.mac.com/gallenx/88E3D706.gpgkey
>
>
// Hisaoki "Yuki" Nishida
// <email@hidden>
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.