macOS/iOS API解説

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

目次

representationUsingType:properties:

指定した画像フォーマットにエンコードしたデータを作ります
-(NSData *)representationUsingType:(NSBitmapImageFileType)storageType:
                    properties:(NSDictionary *)properties:

解説

指定した画像フォーマットにエンコードしたデータを作ります。
● NSTIFFFileType TIFFフォーマット
● NSBMPFileType BMPフォーマット
● NSGIFFileType GIFフォーマット
● NSJPEGFileType JPEGフォーマット
● NSPNGFileType PNGフォーマット

返り値

( NSData * )

エンコードされた画像データ

引数

( NSBitmapImageFileType )storageType

画像フォーマット

( NSDictionary * )properties

エンコードプロパティ

フレームワーク

ApplicationKit

クラス

NSBitmapImageRep

Instance Methods

使用可能

10.0

参照

例文

//PNGフォーマット
data = [imgRep representationUsingType:NSPNGFileType properties:[NSDictionary
dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:2.0],
NSImageCompressionFactor, nil]];
//GIFフォーマット1
data = [imgRep representationUsingType:NSGIFFileType properties:nil];
//GIFフォーマット2
data = [imgRep representationUsingType:NSGIFFileType properties:[NSDictionary 
dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],NSImageDitherTransparency, nil]];

#import "SetImage.h"
//testPNGdata.pngのファイルが警告無しで下記変わるので注意!!!!
@implementation SetImage

- (IBAction)set:(id)sender
{

NSString *path = @"~/testPNGdata.png";//ユーザーディレクトリのトップのtestPNGdata.pngとい
    //開けるファイル拡張子の配列
    NSArray      *imgTypes    = [ NSArray arrayWithObjects : @"tiff",@"tif",nil ];
    //OpenPanelを作る
    NSOpenPanel  *opImage       = [ NSOpenPanel openPanel ];
    //Imageを作る
    NSImage      *img = [[[NSImage alloc] initWithSize:NSMakeSize(100,100)] autorelease];
    NSBitmapImageRep *bmpRep;
    NSData *dat;
NSData *PNGdata;
    //OpenPanelの結果のボタン番号
    int		  opRet;
     
        //OpenPanelでファイル選択   
    opRet = [ opImage runModalForDirectory : NSHomeDirectory() //どこのディレクトリを出すか
                                     file : @"Pictures" //どのどのファイルを選択しておくか
                                    types : imgTypes ];//選べるファイルタイプ

    if ( opRet == NSOKButton ) {  // OPENPanelのボタンがOKなら
        //NSDataを作ってファイルから読み込む
        dat = [NSData dataWithContentsOfFile: [ opImage filename ] ];
         bmpRep =[[[NSBitmapImageRep alloc] autorelease] initWithData:dat];
        [img addRepresentation:bmpRep];
                [image setImage:img];
//PNGデータを作る
PNGdata = [bmpRep representationUsingType:NSPNGFileType properties:[NSDictionary
dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:2.0],
NSImageCompressionFactor, nil]];

//書き出す
if ([PNGdata writeToFile:[path stringByExpandingTildeInPath] atomically:YES]){
    NSLog(@"YES");
}else{
    NSLog(@"NO");
}


     }
}

@end