Re: Why does this leak memory?
Re: Why does this leak memory?
- Subject: Re: Why does this leak memory?
- From: Matt Ball <email@hidden>
- Date: Sun, 10 Jul 2005 20:12:04 -0400
I was asked to post more detailed code. Here is where the function is called:
- (void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell
forTableColumn:(NSTableColumn *)tableColumn row:(int)row
{
int indexToQueryForImage = row * 6 + 1;
NSImage *image = [layersArray objectAtIndex:indexToQueryForImage];
image = [self shadowedImageWithImage:image];
if(image != nil)
[cell setImage:image];
}
- (IBAction)coreImageButtonClicked:(id)sender
{
}
- (NSImage *)shadowedImageWithImage:(NSImage *)image
{
[image setFlipped:YES];
// Resize the image, and make sure to antialias it.
float thumbnailHeight = [tableView rowHeight] * 0.85;
float thumbnailWidth = (thumbnailHeight / [image size].height) *
[image size].width;
NSImage *resizedImage = [[NSImage alloc]
initWithSize:NSMakeSize(thumbnailHeight, thumbnailWidth)];
[resizedImage lockFocus];
[NSGraphicsContext saveGraphicsState];
[[NSGraphicsContext currentContext]
setImageInterpolation:NSImageInterpolationHigh];
[image drawInRect:NSMakeRect(0.0, 0.0, thumbnailWidth,
thumbnailHeight) fromRect:NSMakeRect(0.0, 0.0, [image size].width,
[image size].height) operation:NSCompositeCopy fraction:1.0];
[NSGraphicsContext restoreGraphicsState];
[resizedImage unlockFocus];
image = [resizedImage retain];
[resizedImage release];
// Place our new image on a background
NSImage *imageCanvas = [[NSImage alloc] init];
[imageCanvas setSize:[image size]];
[imageCanvas lockFocus];
NSRect imageFrame = NSMakeRect(0, 0, [imageCanvas size].width,
[imageCanvas size].height);
NSImage *transparencyPattern = [[NSImage alloc] initWithSize:NSMakeSize(4, 4)];
[transparencyPattern lockFocus];
[[NSColor colorWithCalibratedWhite:0.8 alpha:1.0] set];
NSRectFill(NSMakeRect(0,0,2,2));
NSRectFill(NSMakeRect(2,2,2,2));
[[NSColor whiteColor] set];
NSRectFill(NSMakeRect(2,0,2,2));
NSRectFill(NSMakeRect(0,2,2,2));
[transparencyPattern unlockFocus];
[[NSColor colorWithPatternImage:transparencyPattern] set];
NSRectFill(imageFrame);
[image compositeToPoint:NSMakePoint(0, 0) operation:NSCompositeSourceOver];
[imageCanvas unlockFocus];
image = [imageCanvas copy];
[transparencyPattern release];
[imageCanvas release];
NSImage *shadowCanvas = [[NSImage alloc]
initWithSize:NSMakeSize([image size].width+4, [image size].height+4)];
[shadowCanvas lockFocus];
NSShadow *theShadow = [[NSShadow alloc] init];
[theShadow setShadowColor:[NSColor blackColor]];
[theShadow setShadowBlurRadius:4];
[theShadow set];
[image compositeToPoint:NSMakePoint(2, 1) operation:NSCompositeSourceOver];
[shadowCanvas unlockFocus];
image = [shadowCanvas retain];
[theShadow release];
[shadowCanvas release];
return image;
}
And here is the cell's setImage:
- (void)setImage:(NSImage *)anImage
{
if(anImage != image && anImage != nil)
{
image = [anImage retain];
[anImage release];
}
}
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
// Draw the thumbnail image
if (image != nil) {
NSSize imageSize;
NSRect imageFrame;
imageSize = [image size];
NSDivideRect(cellFrame, &imageFrame, &cellFrame, 10 +
imageSize.width, NSMinXEdge);
if ([self drawsBackground]) {
[[self backgroundColor] set];
NSRectFill(imageFrame);
}
imageFrame.origin.x += 3;
imageFrame.size = imageSize;
imageFrame.origin.y = NSMinY(cellFrame) + (cellFrame.size.height -
[image size].height)/2;
[image drawInRect:imageFrame fromRect:NSMakeRect(0, 0, [image
size].width, [image size].height) operation:NSCompositeSourceOver
fraction:1.0];
}
// Modify the cell frame to compensate for the image
cellFrame.origin.y = NSMinY(cellFrame) + cellFrame.size.height/4 + 2;
if([self isHighlighted])
[self setTextColor:[NSColor alternateSelectedControlTextColor]];
else
[self setTextColor:[NSColor textColor]];
// Draw the cell's text normally
[super drawWithFrame:cellFrame inView:controlView];
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden