0

NSBitmapImageRep を指定されたサイズにサイズ変更することを目的とした、次の目的の C 関数があります。

現在、サイズが 2048x1536 の画像を操作して 300x225 にサイズ変更しようとすると、この関数はサイズ 600x450 の NSBitmapImageRep を返し続けます。

- (NSBitmapImageRep*) resizeImageRep: (NSBitmapImageRep*) anOriginalImageRep toTargetSize: (NSSize) aTargetSize
{
    NSImage* theTempImageRep = [[[NSImage alloc] initWithSize: aTargetSize ] autorelease];
    [ theTempImageRep lockFocus ];
    [NSGraphicsContext currentContext].imageInterpolation = NSImageInterpolationHigh;
    NSRect theTargetRect = NSMakeRect(0.0, 0.0, aTargetSize.width, aTargetSize.height);
    [ anOriginalImageRep drawInRect: theTargetRect];
    NSBitmapImageRep* theResizedImageRep = [[[NSBitmapImageRep alloc] initWithFocusedViewRect: theTargetRect ] autorelease];
    [ theTempImageRep unlockFocus];

    return theResizedImageRep;
}

それをデバッグすると、theTargetRect が適切なサイズであることがわかりますが、initWithFocusedRec への呼び出しは 600x450 ピクセル (高さ x 幅) のビットマップを返します

なぜこれが起こっているのか、私は完全に途方に暮れています。誰にも洞察力がありますか?

4

1 に答える 1

1

あなたのテクニックでは、サイズ変更された画像は生成されません。まず、このメソッドinitWithFocusedViewRect:は、フォーカスされたウィンドウからビットマップ データを読み取り、スクリーン グラブを作成するために使用されます。

目的のサイズの新しい NSBitmapImageRep または NSImage を使用して新しいグラフィックス コンテキストを作成し、そのコンテキストに画像を描画する必要があります。

このようなもの。

NSGraphicsContext* context = [NSGraphicsContext graphicsContextWithBitmapImageRep:theTempImageRep];

if (context)
{
    [NSGraphicsContext saveGraphicsState];
    [NSGraphicsContext setCurrentContext:context];

    [anOriginalImageRep drawAtPoint:NSZeroPoint];
    [anOriginalImageRep drawInRect:theTargetRect];

    [NSGraphicsContext restoreGraphicsState];
}
// Now your temp image rep should have the resized original.
于 2016-01-05T23:21:01.063 に答える