Hi everyone, I have a NSView in which I am drawing a waveform from a buffer. This NSView has a child playhead element, that I want to move across the waveform as it plays. The problem is, drawRect: is expensive and anytime the playhead moves, the waveform has to draw itself all over again, causing terrible performance problems.
My thought was to draw the waveform once, save it as an NSImage, and then have drawRect: draw itself from the saved image, and when a new buffer is loaded, discard the image and let drawRect: draw the new waveform and save it as an NSImage, etc. However, when drawRect: is called subsequent times (after new buffers are set), the image appears to be garbage and therefore this strategy is not quite working as planned... What am I doing wrong? @property (nonatomic, strong) NSImage *image; ... -(void)drawRect:(NSRect)dirtyRect { if (self.image) { [self.image drawInRect:dirtyRect]; return; } [[NSColor blackColor] setFill]; NSRectFill(dirtyRect); [super drawRect:dirtyRect]; float zero = self.bounds.size.height / 2; [[NSColor blueColor] set]; NSPoint pointA = NSMakePoint(0, zero); NSPoint pointB = NSMakePoint(self.bounds.size.width, zero); [self drawLineFromPointA:pointA toPointB:pointB]; if (self.buffer) { float spacing = self.bounds.size.width / self.buffer.size; for (int i = 0; i < self.buffer.size - 1; i++) { NSPoint pointA = NSMakePoint(i * spacing, zero - (self.buffer.samples[i] * zero)); NSPoint pointB = NSMakePoint((i + 1) * spacing, zero - (self.buffer.samples[i + 1] * zero)); [self drawLineFromPointA:pointA toPointB:pointB]; } } [self lockFocus]; NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:[self bounds]]; self.image = [[NSImage alloc] initWithData:[rep TIFFRepresentation]]; [self unlockFocus]; } -(void)setBuffer:(Buffer *)buffer { _buffer = buffer; self.image = nil; [self setNeedsDisplay:YES]; } I tried asking this question on Stack Overflow, but it's gotten no love.. http://stackoverflow.com/questions/43177850/trying-to-use-cached-image-as-background-in-nsview Patrick J. Collins http://collinatorstudios.com _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com