Dibujo de una imagen en un NSView
-
12-09-2019 - |
Pregunta
Tengo un sencillo programa Obj C que, por el momento, le permite cargar una imagen, lo elabora, y teóricamente debe permitir ampliar y rotar. Estoy usando NSAffineTranslations.
Quiero que la imagen se bloquea en la parte superior izquierda (en comparación con el estándar PS / PDF de abajo a la izquierda), así que estoy usando isFlipped, y llamando [Aftrans scaleXBy: 1.0 Yby: -1.0];
El problema es que, por alguna razón, después de la primera vez que mi drawRect se llama, la transformación no sucede.
Cuando me carga una imagen, se trata, y parece ser correcta. Si cambio el tamaño de la ventana (que llama a drawRect), dibuja la imagen, pero es al revés y al revés. Esto significa que la transformación no entró en vigor. No veo ninguna diferencia en cualquiera de los datos del segundo tiempo a través.
aquí es una versión simplificada del código:
- (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: aquí hay algo más de código que pueden ser relevantes
.-(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];
}
Solución
No estoy seguro exactamente lo que estamos tratando de lograr, pero si quieres simplemente dibujar una imagen en un NSView y mantenerlo en la parte superior izquierda, se puede hacer algo un poco más simple, en la subclase 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];
}
También tuve la oportunidad de obtener resultados similares con su código mediante la eliminación de las llamadas [self setFrame:...];
en los métodos y drawRect setImage.
Otros consejos
Suponiendo que no está excluyendo código que cambia las cosas, ten cuidado si va a dibujar la imagen en otro lugar. elementos de la barra, mesas, etc., así pueden cambiar la propiedad -flipped de la imagen, haciendo que se dibuja de forma incorrecta.
Probar alrededor de su línea de trazado de la imagen:
BOOL wasFlipped = [image isFlipped];
[image setFlipped:[self isFlipped]];
[image drawInRect:drawingRect
fromRect:imageRect
operation:NSCompositeSourceOver
fraction:1];
[image setFlipped:wasFlipped];
A ver si eso ayuda. Si hace , busca otro en su código para cambiar algo volteado la propiedad de la imagen y no ponerlo de nuevo.