Re: Dock Icon Help
Re: Dock Icon Help
- Subject: Re: Dock Icon Help
- From: Ryan Stevens <email@hidden>
- Date: Fri, 9 May 2003 22:51:33 -0700
On Friday, May 9, 2003, at 04:08 PM, j o a r wrote:
On Friday, May 9, 2003, at 19:54 Europe/Stockholm, Ryan Stevens wrote:
There's really no reason (that I can see) to avoid using +imageNamed
in favor of -initWithContentsOfFile unless you put the image in some
special place that +imageNamed can't find.
The reason I used the "initWithContents..." method in the last email
(as did the poster before me) is that it tries to create image reps
from the data immediately, while the "initByReferencing..." uses lazy
initialization. If you use "initWithContents..." it returns nil if no
image reps can be created from the image data - while using
"initByReferencing..." you cannot be sure that the image returned will
ever be able to create image reps from the data it was pointed to.
Read the docs for more info.
Ok, if we were seriously trying to troubleshoot the issue we would
"initByReferencing..." and then...
if (image != nil) {
if ([image isValid]) // do your thing
else
NSLog(@"image is invalid");
} else
NSLog(@"image is just plain nil");
Because "initWithContents..." could give us a nil leaving no way to
figure out what happened; Couldn't it find the file or did it find a
file it can't do anything with? Where "initByReferencing" will be nil
if the file isn't found(I'm assuming) and invalid if it can't make a
rep. from the data.
Since I'm not sure which init method "imageNamed:" uses internally, I
thought that it would be best to explicitly use the
"initWithContents..." when troubleshooting the problem that the guy
who started this thread had.
I like this (more compact) way..
While "compact" might be considered cool - it's not often the best way
to provide sample code on mailing lists, where it's better to be
overly clear of what you're trying to illustrate.
Your code just had too many extra brackets! :-p
- (void)setDockIcon
{
NSImage *coolImage = [NSImage imageNamed:@"2d Marine Division"];
if (coolImage != nil) [NSApp setApplicationIconImage:coolImage];
else
NSLog(@"Failed to set the Dock icon. Woops!");
}
Also note that if you use "imageNamed:" you should specify the file
name including the file name suffix when creating an image from an
image file (and not just fetching a named image) - at least according
to the docs.
That should've been in your first reply. Should've been in mine though
too...woops.
Obviously imageNamed might not find your file if you exclude the suffix
and it will almost surely take longer without it. Common sense. Plus,
the docs say to include the suffix, silly! ;-)
All that out of the way, I am enjoying this thread. :-)
_______________________________________________
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.