CGImage failing to animate
CGImage failing to animate
- Subject: CGImage failing to animate
- From: Colin Klipsch <email@hidden>
- Date: Sat, 27 Jul 2002 12:39:12 -0400
Greetings.
I'm trying to have a CoreGraphics CGImage of offscreen pixels animate
inside a customized NSView. The View draws the image statically from
its 'drawRect' method -- when the OS calls for a refresh -- and also
refreshes itself periodically in response to an NSTimer driving the
animation.
But it isn't working. The initial frame of animation is being drawn
correctly. After that, the View just displays the same initial frame,
regardless of how the underlying pixels are changed. It's as if the
first display of the image puts it in a cache somewhere, and thereafter,
the offscreen pixel values are ignored.
The animation /does/ work correctly when I don't use CoreGraphics, but
instead use an NSBitmapImageRep, along with the appropriate lines of
supporting code. The problem therefore must be in my use or misuse of
CoreGraphics. (And yes, I need the CoreGraphics way I'm afraid; I can't
revert to the older technique.)
Given the following "prolog":
enum { kWidth = 400, kHeight = 300 };
static UInt8 gPixels[kHeight][kWidth];
static UInt8 gColorTable[16*3] = { /*... 48 RGB values*/ };
static CGImageRef gImage;
static void
releasePixels(void*, const void*, size_t)
{ /*intentionally empty*/ }
I create the CGImage like so:
gImage = CGImageCreate(kWidth, kHeight,
8, //size_t bitsPerComponent
8, //size_t bitsPerPixel
kWidth, //size_t bytesPerRow
CGColorSpaceCreateIndexed(
CGColorSpaceCreateDeviceRGB(),
16-1, gColorTable
),
kCGImageAlphaNone,
CGDataProviderCreateWithData(
nil, gPixels, sizeof(gPixels), &releasePixels
),
nil, //const float decode[]
false, //shouldInterpolate
kCGRenderingIntentDefault); //CGColorRenderingIntent
My custom NSView's 'drawRect' method is:
- (void)drawRect:(NSRect)dirtyRect
{
CGRect rect = {0, 0, kWidth, kHeight};
CGContextRef context = [[NSGraphicsContext
graphicsContextWithWindow:[self window]] graphicsPort];
CGContextDrawImage(context, rect, gImage);
CGContextSynchronize(context); //need??
CGContextFlush(context); //need??
}
And the NSTimer's callback, which invokes each frame of animation, goes
like:
- (void)fireTimer:(NSTimer*)aTimer
{
// fiddle with the gPixels array
[myView setNeedsDisplay:YES];
}
So, any ideas why I'm not getting animation beyond the drawing of the
first frame (which renders correctly)?
Thanks to anyone who responds.
-- Colin K.
_______________________________________________
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.