nsview에서 이미지를 그리십시오
-
12-09-2019 - |
문제
현재 이미지를로드하고, 그릴 수 있으며, 이론적으로 확대하고 회전 할 수있는 간단한 OBJ C 프로그램이 있습니다. 나는 nsaffinetranslations를 사용하고 있습니다.
이미지가 왼쪽 상단에 고정되기를 원합니다 (왼쪽 하단의 PS/PDF 표준과 달리). ISFLIPPED를 사용하고 [AFTRANS SCALEXBY : 1.0 YBY : -1.0]을 호출하고 있습니다.
문제는 어떤 이유로, 처음으로 내 drawRect가 호출 된 후에는 변환이 일어나지 않는다는 것입니다.
이미지를로드하면 나타나서 정확해 보입니다. 창의 크기를 변경하면 (DrawRect 호출) 이미지가 그리지만 거꾸로되면 반전됩니다. 이것은 변환이 적용되지 않았 음을 의미합니다. 나는 두 번째 데이터의 차이가 없다.
다음은 코드의 제거 버전입니다.
- (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 : 여기에 관련된 코드가 더 있습니다.
-(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];
}
해결책
나는 당신이 달성하려는 것을 정확히 잘 모르겠지만, nsview에 이미지를 그리고 왼쪽 상단에 유지하려면 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];
}
나는 또한 당신의 코드로 비슷한 결과를 얻을 수있었습니다. [self setFrame:...];
DrawRect 및 SetImage 방법으로 호출.
다른 팁
물건이 변경되는 코드를 제거하지 않는다고 가정하면 다른 곳에서 이미지를 그리는 지 확인하십시오. 도구 모음 항목, 테이블 등은 이미지의 플라이팅 속성을 잘 변경하여 잘못으로 끌어 당길 수 있습니다.
이미지 드로잉 라인 주변에서 시도해보십시오.
BOOL wasFlipped = [image isFlipped];
[image setFlipped:[self isFlipped]];
[image drawInRect:drawingRect
fromRect:imageRect
operation:NSCompositeSourceOver
fraction:1];
[image setFlipped:wasFlipped];
그것이 도움이되는지 확인하십시오. 그 경우 하다, 이미지의 뒤집힌 속성을 바꾸고 다시 넣지 않는 것에 대해 코드의 다른 곳을 살펴보십시오.