NSViewの中に画像を描画します
-
12-09-2019 - |
質問
私は、現時点では、あなたがイメージをロードすることができます、簡単なOBJのCプログラムを持って、それを描画し、理論的にはあなたがズームして、回転させてください。私はNSAffineTranslationsを使用しています。
; [-1.0:1.0 yByのafTrans scaleXBy]は、私が(左下のPS / PDFの規格とは対照的に)画像が左上にロックすることにしたいので、私はisFlippedを使用して、と呼んでいますP>
問題は、何らかの理由で、私のdrawRectが呼び出された最初の時間の後に、変換が起こらないということです。
私は画像をロードすると、、それがアップしますし、正しい見えます。私は(のdrawRectを呼び出す)、ウィンドウのサイズを変更すると、画像が描画しますが、逆さま、逆です。これは、変換が有効にならなかったことを意味します。私は2回目を介したデータのいずれかの違いが表示されません。
ここでのコードのストリップダウンバージョンです
- (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];
}
私ものdrawRectとsetImage方法で[self setFrame:...];
呼び出しを取り除くことで、あなたのコードと同様の結果を得ることができた。
他のヒント
あなたが他の場所で画像を描画しているかどうかを見て、あなたがものを変更するコードを除去していないと仮定。ツールバー項目、テーブルなども、それは間違って描画させ、画像の-flippedプロパティを変更することがあります。
あなたのイメージの描画線の周りにこれを試してください:
BOOL wasFlipped = [image isFlipped];
[image setFlipped:[self isFlipped]];
[image drawInRect:drawingRect
fromRect:imageRect
operation:NSCompositeSourceOver
fraction:1];
[image setFlipped:wasFlipped];
それは場合に役立ちます。を参照してください。それはののない場合は、画像の反転したプロパティを変更する何かのために他の場所で、あなたのコード内で見て、それをバック入れていない。