Re: erasing, redrawing NSBezierPath into and NSImage problem
Re: erasing, redrawing NSBezierPath into and NSImage problem
- Subject: Re: erasing, redrawing NSBezierPath into and NSImage problem
- From: Wilhelm Phillips <email@hidden>
- Date: Wed, 7 Jul 2004 12:13:37 -0700
Hi Louis,
Thanks for clarifying what has really been happening in my code.
And thanks for taking the time to suggest more correct and efficient
approaches which I am now using in my app. I need to be more careful
with memory management.
At one point, I did include [someLabel release] in my code but it
seemed to crash on redrawing my labelMatrix. Perhaps, because I did
not [someLabel setName: nil] first as you explain in your setName:
paragraph below?
Will
On Jul 7, 2004, at 4:14 AM, Louis C. Sacha wrote:
Hello...
You are still leaking an NSImage each time through this method, since
you create a new NSImage instance but don't release the old one.
You're also not really erasing anything when you fill the bezier path
with the clear color, since you've just created a brand new image,
which is empty.
The reason why setName: was not working before was that (after the
first time through the method) there was already an existing image
with that name, and you were trying to register a new image (the one
created in the current pass through the method) with the same name.
Once you added the line to unregister the old image, you were able to
register the new image with that name.
I would suggest trying one of the following approaches:
/* typed in mail, not tested, etc... */
- (void)drawLabels
{
// bezierPath drawing here, etc.
/* unregisters the old image/name and releases the existing image (if
the image exists) */
[someLabel setName: nil];
[someLabel release];
/* this line creates a new NSImage instance */
someLabel = [[NSImage alloc] initWithSize:NSMakeSize(cellWidth,
cellHeight)];
[someLabel lockFocus];
/* draw the label in the newly created image */
[[NSColor myNewColor] set];
[bezierPath fill];
[someLabel unlockFocus];
/* registers the new image with name */
[someLabel setName:@"Some Label Name"];
}
or
- (void)drawLabels
{
// bezierPath drawing here, etc.
NSRect imageRect = NSMakeRect(0.0f, 0.0f, cellWidth, cellHeight);
if (!someLabel)
{
/* only creates a new image if someLabel doesn't already exist */
someLabel = [[NSImage alloc] initWithSize:imageRect.size];
}
else
{
/* otherwise just resize the existing image for the new size */
[someLabel setSize:imageRect.size];
//[someLabel setName: nil]; /* probably don't need this line */
}
[someLabel lockFocus];
/* erase the existing image */
[[NSColor clearColor] set];
NSRectFill(imageRect);
/* draw the new label */
[[NSColor myNewColor] set];
[bezierPath fill];
[someLabel unlockFocus];
//[someLabel setName:@"Some Label Name"]; /* probably don't need this
line */
}
For the second version, I don't think you need the two setName:
messages, although it's possible that there is some sort of caching
going on in the cells of your matrix that requires it.
Hope that helps,
Louis
...
- (void)drawLabels
{
// bezierPath drawing here, etc.
[someLabel setName: nil]; // added this to fix the problem
someLabel = [[NSImage alloc] initWithSize:NSMakeSize(cellWidth,
cellHeight)];
[someLabel lockFocus];
[[NSColor clearColor] set];
[bezierPath fill]; // this actually does clear bezierPath from
someLabel correctly
// in the same way that Louis' NSRectFill with clearColor
suggestion works
[[NSColor myNewColor] set];
[bezierPath fill];
[someLabel setName:@"Some Label Name"];
[someLabel unlockFocus];
}
So, by setting the name to nil first and then renaming it after I drew
into it, my labelMatrix updates correctly and everything works as I
expected. This must be a very fundamental issue that I somehow
missed.
Will
_______________________________________________
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.
_______________________________________________
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.