problem with NSAffineTransform...
problem with NSAffineTransform...
- Subject: problem with NSAffineTransform...
- From: "Michael B. Johnson" <email@hidden>
- Date: Fri, 21 Sep 2001 00:27:06 -0700
So this came up when I was writing up a minimal example to show how to
zoom an image without scaling the NSImage, but rather just scaling the
view.
I have a nice simple example (see below - just add to a project with a
"test" image, and a window with a "ZoomView" on it), but unfortunately, if
I initialize my NSAffineTransform with:
_zoomTransform = [NSAffineTransform transform];
as opposed to:
_zoomTransform = [[NSAffineTransform alloc]
initWithTransform:[NSAffineTransform transform]];
then it draws correctly the first time, but then the NSAffineTransform
pointed to by _zoomTransform gets replaced with an NSString*. The value
of the pointer isn't changing, but what it points to changes out from
underneath me. I tried to use MallocDebug on it, but truthfully, I wasn't
sure how to incorporate libMallocDebug.xxx into my app (there's no
framework, and well, in the new world order, I'm not sure what to do with
a shared library that's not in a framework...)
Is this something silly I'm doing, or is there a bug in using the
transform you get back from the class? I'll admit to not being completely
sure what to make of instances that I get back from the class object...
ZoomView.h:
#import <Cocoa/Cocoa.h>
@interface ZoomView : NSView
{
NSImage* _myImage;
NSAffineTransform* _zoomTransform;
}
@end
ZoomView.m:
#import "ZoomView.h"
@implementation ZoomView
- (id)initWithFrame:(NSRect)frameRect
{
[super initWithFrame:frameRect];
// this one will crash:
//_zoomTransform = [NSAffineTransform transform];
// this one works fine:
_zoomTransform = [[NSAffineTransform alloc]
initWithTransform:[NSAffineTransform transform]];
NSLog(@"_zoomTransform's retainCount == %d\n", [_zoomTransform
retainCount]);
return self;
}
- (void)dealloc
{
[_zoomTransform release];
[super dealloc];
return ;
}
- (void)awakeFromNib
{
_myImage = [NSImage imageNamed:@"test"];
[_myImage setScalesWhenResized:YES];
[_myImage setDataRetained:YES]; // so we can recreate it when zooming
if it's a PDF
return ;
}
- (void)drawRect:(NSRect)aRect
{
NSSize size = [_myImage size];
NSRect srcRect;
srcRect.origin.x = 0.0f;
srcRect.origin.y = 0.0f;
srcRect.size.width = size.width;
srcRect.size.height = size.height;
NSLog(@"_zoomTransform == %p\n", _zoomTransform);
[_zoomTransform concat];
[_myImage drawAtPoint:srcRect.origin
fromRect:srcRect
operation:NSCompositeSourceOver
fraction:1.0f];
return ;
}
- (void)zoom:(float)scale
{
[_zoomTransform scaleBy:scale];
[self setNeedsDisplay:YES];
return ;
}
- (BOOL)acceptsFirstResponder
{
return YES;
}
- (void)keyDown:(NSEvent *)theEvent
{
NSString* eventKeys = [theEvent characters];
unsigned int keyCount = [eventKeys length];
unsigned int keyIndex;
for (keyIndex = 0; keyIndex < keyCount; keyIndex++) {
switch ([eventKeys characterAtIndex:keyIndex]) {
case 'z': [self zoom:1.1f];
break ;
case 'Z': [self zoom:0.9f];
break ;
default: break;
}
}
return ;
}
@end
--> Michael B. Johnson, Ph.D. -- email@hidden
--> Studio Tools, Pixar Animation Studios
-->
http://xenia.media.mit.edu/~wave