Re: optimizing background image drawing with -drawRect
Re: optimizing background image drawing with -drawRect
- Subject: Re: optimizing background image drawing with -drawRect
- From: "I. Savant" <email@hidden>
- Date: Mon, 2 Apr 2007 14:31:21 -0400
On 4/2/07, Artemiy Pavlov <email@hidden> wrote:
My guess is I should load and scale the image once, and use -drawRect
to only do drawAtPoint. But the problem is I cannot move this part of
the code outside -drawRect, I get "error: initializer element is not
constant" for lines with NSString, NSImage, etc.
You guessed correctly - you should cache the image.
I've actually never encountered the error you've mentioned. I guess
it depends how you're trying to "move the code outside" -drawRect: ...
I'd do it this way:
In your interface, declare an image:
NSImage * cachedImage;
Then declare a method to get the image:
- (NSImage *)cachedImage;
In your implementation:
- (NSImage *)cachedImage
{
if (!cachedImage)
cachedImage = ...; // get/assign image here.
return cachedImage;
}
In your -drawRect: method, don't use the cachedImage pointer
directly (like [cachedImage someMessage]) but rather always get it via
the method above ([[self cachedImage] someMessage]... that way, the
image is cached the first time it's called and the cache is used from
then on.
Hope this helps.
--
I.S.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden