NSImage:drawInRect:fromRect:operation:fraction:
Home 目次>ApplicationKit>NSImage

drawInRect:fromRect:operation:fraction:

部分的に合成方法、範囲を指定して画像を合成します


-(void) drawInRect:(NSRect)rect
   fromRect:(NSRect)fromRect
   operation:(NSCompositingOperation)op
   fraction:(float)delta


【返り値】
   voidなし
【パラメータ】
   rect表示先範囲
   fromRect表示元範囲
   op合成方法
   delta不透明度


【解説】

部分的に合成方法、範囲を指定して画像を合成します。(NSAffineTransformで回転を適用可能)
【NSCompositingOperation】合成方法
● NSCompositeClear クリアする
● NSCompositeCopy 透明度を無視して上書きする
● NSCompositeSourceOver 透明度を考慮してして上書きする
● NSCompositeDestinationOver
● NSCompositeSourceIn 透明度を無視して上書きする
● NSCompositeDestinationIn 色があるところは切り抜き
● NSCompositeSourceOut クリアする
● NSCompositeDestinationOut 色があるところはクリア
● MSCompositeSourceAtop 透明度を考慮してして上書きする
● NSCompositeDestinationAtop
● NSCompositeXOR
● NSCompositePlusDarker
● NSCompositeHighlight
● NSCompositePlusLighter

NSAffineTransformで回転させる場合。
変換マトリックを作って、その後フォーカスしているビューにsetする。
NSAffineTransform *affin = [NSAffineTransform transform];

[affin rotateByDegrees:10.0];
[affin set];
[thePath transformUsingAffineTransform: affin];



【例文】


#import "SetImage.h"

@implementation SetImage

- (IBAction)set:(id)sender
{
//開けるファイル拡張子の配列
NSArray *imgTypes = [ NSArray arrayWithObject : @"tiff" ];
//OpenPanelを作る
NSOpenPanel *opImage = [ NSOpenPanel openPanel ];
//Imageを作る
NSImage *img;
NSImage *img2;
//OpenPanelの結果のボタン番号
int opRet;

//OpenPanelでファイル選択
opRet = [ opImage runModalForDirectory : NSHomeDirectory() //どこのディレクトリを出すか
file : @"Pictures" //どのどのファイルを選択しておくか
types : imgTypes ];//選べるファイルタイプ

if ( opRet == NSOKButton ) { // OPENPanelのボタンがOKなら
//NSImageを作ってファイルから読み込む
img = [ [ NSImage alloc ]
initWithContentsOfFile: [ opImage filename ] ];
//NSImageをバンドルファイルからつくる
img2 = [NSImage imageNamed: @"NSApplicationIcon" ];

//imgを描画対象にする
[img lockFocus];
//描画対象にimg2を合成する
[img2 drawInRect:NSMakeRect(100.0,100.0,160.0,160.0)
fromRect:NSMakeRect(0.0,0.0,80.0,80.0)
operation:NSCompositeXOR
fraction:0.5
];
//合成後のimgをImageViewに表示する
[image setImage:img];
//imgを描画から外す
[img unlockFocus];
}
}

@end


(C) 2000-2007 Satoshi Oomori.
[Apple]

Google
Webwww.oomori.com
Apple_Store_40x120

[ad:Usual day in Japan]

この記事を評価してください。 

良い 

間違いがある 説明がわかりにくい 例文がわかりにくい  

Home 目次>ApplicationKit>NSImage
修正日2006.12.26