Re: Optimizing drawing by caching
Re: Optimizing drawing by caching
- Subject: Re: Optimizing drawing by caching
- From: Shawn Erickson <email@hidden>
- Date: Sun, 29 Sep 2002 08:41:57 -0700
On Sunday, September 29, 2002, at 08:18 AM, Drew McCormack wrote:
>
Since what is drawn changes infrequently, I thought I should cache the
>
drawing in an NSImage or something like that. Rather than draw to
>
screen, I would draw to the image, and then draw the image on the
>
screen whenever needed. I thought the best place to do the caching
>
would be the "display" method: this could rebuild the image if
>
necessary, and then call the super's display, which in turn would call
>
"drawRect:" where the image would actually be drawn on the screen.
>
>
Is this approach a good idea, or am I missing something? Are there any
>
pitfalls? Does anyone know any sample code for this? Can anyone give
>
me any idea what NSImage or NSBitmapImageRep classes I should use for
>
this purpose?
Its easy to do as an example...
- (void)drawRect:(NSRect)rect
{
[graphImage compositeToPoint:NSMakePoint(0,0)
operation:NSCompositeCopy];
//other image drawing methods could be used based on your needs
//one could also do transforms on the cached image as needed
//(say to support fit to window, actual size, etc. type display modes)
}
- (void)drawGraph
{
[graphImage lockFocus];
// do your drawing (in this case draw grid lines, data points, etc.)...
[graphImage unlockFocus];
[self setNeedsDisplay:YES];
}
In the above example drawGraph is called periodically (about 1-30 times
a second) depending on incoming data that needs to be graphed. The view
draws is self will drawRect as needed but it only composite the cached
image. This allow the scrolling and resize of the window to go smoothly.
The image I am using was created simply by...
graphImage = [[NSImage alloc] initWithSize:rect.size];
[graphImage setBackgroundColor:[NSColor whiteColor]];
>
One more thing: All my drawn makes use of Quartz, so a postscript
>
format may be my best bet. Could I/Should I use a PDF image rep for
>
this caching, or a bitmap image rep?
If the output goes to the screen in the end then a bitmap would be best.
_______________________________________________
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.