NSImage:initWithContentsOfFile:
Home 目次>ApplicationKit>NSImage

initWithContentsOfFile:

画像ファイルを開いてNSImageを作ります


-(id) initWithContentsOfFile:(NSString *)filename


【返り値】
   id画像
【パラメータ】
   filename画像ファイル名


【解説】

画像ファイルを開いてNSImageを作ります。
-initByReferencingFileと違い、ファイルを開けてデータから一つ以上の画像表示内容(imageRep)を作ります。
ファイル名は拡張子を含む絶対パス、または相対パスです。
初期設定で扱うことの出来るファイル形式はTIFF,GIF,jpg,pdf.pict,epsです。
初期化できなければnilを返します。



【例文】


#import "SetImage.h"

@implementation SetImage

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

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

if ( opRet == NSOKButton ) { // OPENPanelのボタンがOKなら
//NSImageを作ってファイルから読み込む
img = [ [ NSImage alloc ]
initWithContentsOfFile: [ opImage filename ] ];
//レシーバimage(NSImageView)にimgをセットする
[image setImage : img ];
}
}

@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