Improving very slow NSView with cached NSImage
Improving very slow NSView with cached NSImage
- Subject: Improving very slow NSView with cached NSImage
- From: John Nairn <email@hidden>
- Date: Mon, 09 Sep 2002 08:17:49 -0600
Many thanks to all those that sent help about developing a good
interface with an NSView that draws the results of "massive" scientific
calculations and draws them very slowly. I implemented many ideas over
the weekend. The one remaining task is dealing with live resizing and
scrolling. I am continuing to make the drawRect method as fast as
possible, but I doubt it can be improved much. At least my program
draws the image much faster that Adobe illustrator draws a PDF saved
from a slow NSView. Thus, I think the best approach is to cache an
NSImage and use that while resizing and scrolling. This was suggested
by a few people. It almost works, but I think I missing something about
drawing in an NSImage. I searched all NSImage documentation and
"Drawing and Images," but I can't find the detail I need.
Here is my image caching code (adapted from list posting suggestions)
and below is the problem:
// draw image (recreating if needed)
- (void)drawRect:(NSRect)rect
{
if(useImage)
{ if(!cacheImg) [self recache:rect];
[cacheImg drawInRect:rect fromRect:rect
operation:NSCompositeSourceOver fraction:1.0];
}
else
[self recache:rect];
}
// draw mesh plot
- (void)recache:(NSRect)rect;
{
NSSize size;
// release and start over
if(useImage)
{ size= [self bounds].size;
[cacheImg release];
cacheImg = [[NSImage alloc] initWithSize:size];
[cacheImg lockFocus];
}
<< Code removed which just draws the complicated image >>
if(useImage)
{ [cacheImg unlockFocus];
[self setNeedsDisplay:YES];
}
}
I added the useImage BOOL. If this is NO, the NSView draws its image
each time (i.e., old slow method). If this is YES, the NSView creates
an NSImage the first time and uses that on subsequent calls. The code
simply releases cacheImg whenever the plot needs to be recalculated.
When using cacheImg, this code does not work. I am pretty sure it just
my misunderstanding of using NSImage drawing. My complicated NSView has
scaled bounds and the origin is not at (0,0). The code appears to work
when the NSView is not scaled and the origin is at (0,0) (i.e. when
frame and bounds are the same). I thought I could figure out this
scaling effect, but nothing I tried work. I quess the only question is:
Is it possible to draw into an NSView using scaled and translated
coordinates or must the view use unscaled coordinates with the origin
at (0,0)? If it is possible, what needs to be different than when
drawing directly to the NSView? Many parts of my applications use the
actual coordinates and it would very inconvenient to rewrite them all
to use (0,0) based pixels instead.
------------
John Nairn (1-801-581-3413, FAX: 1-801-581-4816)
Web page:
http://www.mse.utah.edu/~nairn
_______________________________________________
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.