views:

332

answers:

1

Let's say I have a code like this...

- (id) init {
    ...
    self.myImage1 = [NSImage imageNamed:@"some_image_name"]; // setter retains
    ...
}

- (void) setStatusItemImage
{
    [self.statusItem setImage:self.myImage1];
}

I also want to animate the status item so I created 6 variants of the "updating" status item icon and loop it over an NSTimer, with target method similar to the above. Questions are:

  1. How can I efficiently assign an image? e.g. is it OK if I use a retained / preloaded NSImage?
  2. Is there a way to animate the status item in a more efficient way? I don't know any Core Animation stuff, so please bear with me.
+5  A: 

As the proverb goes, premature optimization is the root of all evil. Measure first, then optimize whatever's slow, then measure again to make sure it helped.

That said, imageNamed: is slow. You almost always should use NSBundle and -[NSImage initWithContentsOfFile:] instead.

is it OK if I use a retained / preloaded NSImage?

Why would you not? And why would the status item care whether the image is retained by something else or not?

Is there a way to animate the status item in a more efficient way?

setImage: is the only way. As long as you're not loading each image every time, you should have no efficiency problems.

Peter Hosey