1

画像を表示する NSView があり、このビューをトリミング画像効果のように動作させたいと考えています。次に、3つの長方形(imageRectsecRectおよびIntersectRect)を作成しimageRectます。これは、画像を表示する四角形であり、secRect全体を暗くするだけの四角形であり、四角形を観察するような四角形です。私がやりたいことは、「穴」を作るようなものです"上に直接(暗くせずに)見えるようにします。これが私の drawRect メソッドです:imageRectintersectRectsecRectimageRect

- (void)drawRect:(NSRect)rect {
    // Drawing code here.
 NSImage *image = [NSImage imageNamed:@"Lonely_Tree_by_sican.jpg"];
 NSRect imageRect = [self bounds];

 [image compositeToPoint:NSZeroPoint operation:NSCompositeSourceOver ];

 if (NSIntersectsRect([myDrawRect currentRect], [self bounds])) {
  //get the intersectionRect
  intersectionRect = NSIntersectionRect([myDrawRect currentRect], imageRect);

  //draw the imageRect
  [image compositeToPoint:imageRect.origin operation:NSCompositeSourceOver];

  //draw the secRect and fill it with black and alpha 0.5
  NSRect secRect = NSMakeRect(imageRect.origin.x, imageRect.origin.y, imageRect.size.width, imageRect.size.height);
  [[NSColor colorWithCalibratedRed:0.0 green:0.0 blue:0.0 alpha:0.5] set];
  [NSBezierPath fillRect:secRect];

  //have no idea for the intersectRect
  /*[image compositeToPoint:intersectionRect.origin
        fromRect:secLayer
       operation:NSCompositeXOR
        fraction:1.0];*/

 }

 //draw the rectangle
 [myDrawRect beginDrawing];

}

マウスのクリックに基づいて長方形を描画する独自のクラス(myDrawRect)がある[self bounds]ため、コマンドを無視してくださいbeginDrawing

どんな助けでも大丈夫です、ありがとう。へビアン。

4

1 に答える 1

5

必要以上の作業を行っており、非推奨のメソッド (compositeToPoint:operation:およびcompositeToPoint:fromRect:operation:fraction:メソッド) を使用しています。

画像を 1 つのdrawInRect:fromRect:operation:fraction:メッセージで送信するだけです。パラメータは、fromRect:トリミングする四角形です。トリミングされたセクションをスケーリングしたくない場合は、宛先の四角形 (drawInRect:パラメーター) を同じサイズにする必要があります。

必要な唯一の追加作業は、画像がビューよりも大きく、ビューの境界内にあるセクションのみを描画したい場合です: その場合は、トリミング長方形を差で挿入する必要があります。トリミング長方形とビュー境界の間のサイズ。

于 2010-08-25T05:21:25.687 に答える