macOS/iOS API解説

iOS , Mac アプリケーション開発のために使われる主要フレームワークの日本語情報です。2010年代に書かれた内容です。今後更新はありません。

目次

initWithContentsOfFile:

INDEX>Foundation> NSData

パスで指定したファイルからデータを読み取り、初期化して返します

Objective-C

-(id)initWithContentsOfFile:(NSString *)path

Swift


    

解説

パス(path)で指定したファイルからデータを読んで、初期化して返します。
– initWithContentsOfFile:options:error:の第二引数に0、第三引数にNULLを指定したのと同じ。

返り値

Objective-C

( id )

Swift


    

データ

引数

Objective-C

( NSString * )path

Swift


    

ファイルパス

クラス

NSData

使用可能

10.0

参照

- initWithContentsOfMappedFile:
+ dataWithContentsOfFile:

関連記事(外部サイト)

更新時バージョン

OS X 10.10
iOS 8.0

例文

Objective-C

#import "SetImage.h"

@implementation SetImage

- (IBAction)set:(id)sender
{
    //開けるファイル拡張子の配列
    NSArray      *imgTypes    = [ NSArray arrayWithObject : @"tiff" ];
    //OpenPanelを作る
    NSOpenPanel  *opImage       = [ NSOpenPanel openPanel ];
    //Imageを作る
    NSImage      *img = [[[NSImage alloc] initWithSize:NSMakeSize(100,100)] autorelease];
    NSBitmapImageRep *bmpRep;
    NSData *dat;
    //OpenPanelの結果のボタン番号
    int		  opRet;
     
        //OpenPanelでファイル選択   
    opRet = [ opImage runModalForDirectory : NSHomeDirectory() //どこのディレクトリを出すか
                                     file : @"Pictures" //どのどのファイルを選択しておくか
                                    types : imgTypes ];//選べるファイルタイプ

    if ( opRet == NSOKButton ) {  // OPENPanelのボタンがOKなら
        //NSDataを作ってファイルから読み込む
        dat = [[[NSData alloc] autorelease] initWithContentsOfFile: [ opImage filename ] ];
        bmpRep = [NSBitmapImageRep imageRepWithData:dat];
        [img addRepresentation:bmpRep];
        [image setImage:img];
     
        NSLog([NSString stringWithFormat:@"bit / pixel:%d",[bmpRep bitsPerPixel]]);
    }
}

@end

Swift