macOS/iOS API解説

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

目次

drawRepresentation:inRect:

指定した範囲に画像内容を描画します
-(BOOL)drawRepresentation:(NSImageRep *)imageRep:
               inRect:(NSRect)rect:

解説

指定した範囲に画像内容を描画します。

返り値

( BOOL )

YES/NO

引数

( NSImageRep * )imageRep

画像の中身

( NSRect )rect

範囲

フレームワーク

ApplicationKit

クラス

NSImage

Instance Methods

使用可能

10.0

参照

例文

#import "SetImage.h"

@implementation SetImage

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

    if ( opRet == NSOKButton ) {  // OPENPanelのボタンがOKなら
        //NSImageを作ってファイルから読み込む

        img = [NSImage imageNamed: @"NSApplicationIcon" ];
       imgRep = [NSImageRep imageRepWithContentsOfFile:[ opImage filename ]];

        //imgを描画対象にする
        [img lockFocus];

        [img drawRepresentation:imgRep inRect:NSMakeRect(0,0,10,10)];
        
                //合成後のimgをImageViewに表示する
        [image setImage:img];
        //imgを描画から外す
        [img unlockFocus];
    }
}

@end