Drawing an image in NSView

I have a simple Obj C program that at the moment allows you to upload an image, draw it and theoretically let you zoom in and rotate. I am using NSAffineTranslations.

I want the image to be locked in the upper left corner (unlike the PS / PDF standard at the bottom left), so I use isFlipped and call [afTrans scaleXBy: 1.0 yBy: -1.0];

The problem is that for some reason, after calling my drawRect, the conversion does not happen.

When I upload an image, it appears and looks right. If I resize the window (which calls drawRect), the image draws, but it is upside down and canceled. This means that the conversion has not taken effect. I do not see any differences in any of the data for the second time.

here is a stripped down version of the code:

- (void)drawRect:(NSRect)rect { // Drawing code here. // NSLog(@"window type: %d", [[self window] backingType]); NSAffineTransform *afTrans = [[NSAffineTransform alloc] init]; NSGraphicsContext *context = [NSGraphicsContext currentContext]; NSSize sz; NSRect windowFrame = [[self window] frame]; NSRect cv =[[[self window] contentView] frame]; float deltaX, deltaY; NSSize superSize = [[self superview] frame].size; float height, width, sHeight, sWidth; NSRect imageRect; sz = [ image size]; imageRect.size = sz; imageRect.origin = NSZeroPoint; height = sz.height ; width = sz.width ; // sHeight and sWidth are the hieght and with of the super-view. ie, // the size of the whole window view including the space for the // scroll bars, etc, but not including the panel or the borders, sHeight = superSize.height; sWidth = superSize.width; [context saveGraphicsState]; deltaX = 0; deltaY = 0; deltaY += height; // account for flipping [afTrans translateXBy:deltaX yBy:deltaY]; [afTrans scaleXBy:1.0 yBy:-1.0]; [afTrans concat]; NSRect drawingRect = imageRect; NSRect frame = imageRect; [self setFrame:frame]; [image drawInRect:drawingRect fromRect:imageRect operation:NSCompositeSourceOver fraction:1]; [afTrans release]; [context restoreGraphicsState]; } 

ETA: here is another code that MAY be relevant.

 -(void )setImage:( NSImage * )newImage { [newImage retain]; [image release]; rotation = 0; zoom = 1.0; image = newImage; NSSize imageSize = [newImage size]; NSRect tFrame = [self frame]; tFrame = [[self window] frame]; tFrame.size.width = MAX(tFrame.size.width, imageSize.width); tFrame.size.height = MAX(tFrame.size.height, imageSize.height); [self setFrame:tFrame]; [self setNeedsDisplay:YES]; } 
+4
source share
2 answers

I'm not sure exactly what you are trying to achieve, but if you just want to draw an image in NSView and save it in the upper left, you can do something simpler in a subclass of NSView:

 - (BOOL)isFlipped { return YES; } - (void)setImage:(NSImage *)newImage { [newImage retain]; [image release]; image = newImage; [image setFlipped:YES]; [self setNeedsDisplay:YES]; } - (void)drawRect:(NSRect)rect { [image drawAtPoint:NSZeroPoint fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1]; // Or stretch image to fill view //[image drawInRect:[self bounds] fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1]; } 

I was also able to get similar results with your code, getting rid of calls [self setFrame:...]; in the drawRect and setImage methods.

+21
source

Assuming you are not deleting code that changes material, be sure to draw the image elsewhere. Toolbar, table, etc. elements It may well change the -flipped property of the image, causing it to draw incorrectly.

Try this around the line of drawing an image:

 BOOL wasFlipped = [image isFlipped]; [image setFlipped:[self isFlipped]]; [image drawInRect:drawingRect fromRect:imageRect operation:NSCompositeSourceOver fraction:1]; [image setFlipped:wasFlipped]; 

See if that helps. If so, look elsewhere in your code for something to change the property of the inverted image and not return it.

+3
source

All Articles