2

NSview または NSImageView で画像を表示したい。私のヘッダーファイルには

@interface FVView : NSView
{
    NSImageView *imageView;
}
@end

実装ファイルで私がやろうとしていたことは次のとおりです。

- (void)drawRect:(NSRect)dirtyRect
{
    [super drawRect:dirtyRect];

    (Here I get an image called fitsImage........ then I do)

    //Here I make the image
    CGImageRef cgImage = CGImageRetain([fitsImage CGImageScaledToSize:maxSize]);

    NSImage *imageR = [self imageFromCGImageRef:cgImage];
    [imageR lockFocus];

    //Here I have the view context
    CGContextRef ctx = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];

    //Here I set the via dimensions
    CGRect renderRect = CGRectMake(0., 0., maxSize.width, maxSize.height);

    [self.layer renderInContext:ctx];
    [imageR unlockFocus];

    CGContextDrawImage(ctx, renderRect, cgImage);
    CGImageRelease(cgImage);
}

スクリプトを実行しても、NSview ウィンドウに何も表示されません。エラーはまったくありません。何が間違っているのかわかりません。5.1.1 の私の Xcode バージョン

CGImageRef を操作してウィンドウまたは nsview で表示する方法を学習しようとしています。

ありがとうございました。

4

1 に答える 1

4

あなたの設定が正確に何であるかはよくわかりません。カスタム ビューでの画像の描画は、NSImageView. また、レイヤーがサポートされている (またはサポートされていない) カスタム ビューは、レイヤーをホストするビューとは異なります。

適切な要素がたくさんありますが、それらはすべて混同されています。にフォーカスをロックする必要はありませんNSImage。それはに描画するためのものNSImageです。また、からサブクラス化するカスタム ビューはNSViewsuperその-drawRect:. NSView何も描きません。

カスタム ビューで画像を描画するには、次を試してください。

- (void) drawRect:(NSRect)dirtyRect
{
    CGImageRef cgImage = /* ... */;
    NSSize maxSize = /* ... */;
    CGContextRef ctx = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
    CGRect renderRect = CGRectMake(0., 0., maxSize.width, maxSize.height);
    CGContextDrawImage(ctx, renderRect, cgImage);
    CGImageRelease(cgImage);
}

があればNSImageView、カスタム ビューや描画メソッドやコードは必要ありません。画像またはそれを生成するために必要な情報を取得した時点で、次の手順を実行してください。

NSImageView* imageView = /* ... */; // Often an outlet to a view in a NIB rather than a local variable.
CGImageRef cgImage = /* ... */;
NSImage* image = [[NSImage alloc] initWithCGImage:cgImage size:/* ... */];
imageView.image = image;
CGImageRelease(cgImage);

レイヤー ホスティング ビューで作業している場合はCGImage、 をレイヤーのコンテンツとして設定するだけです。繰り返しますが、画像またはそれを生成するために必要な情報を入手するたびに、これを行います。にはありません-drawRect:

CALayer* layer = /* ... */; // Perhaps someView.layer
CGImageRef cgImage = /* ... */;
layer.contents = (__bridge id)cgImage;
CGImageRelease(cgImage);
于 2014-05-21T18:22:04.833 に答える