macOS/iOS API解説

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

目次

+imageRepWithData:

INDEX>AppKit>NSBitmapImageRep> imageRepWithData

データからビットマップ表現を作って返します

Objective-C

+ (instancetype)imageRepWithData:(NSData *)bitmapData

Swift

class func imageRepsWithData(_ data: NSData) -> [NSImageRep]
NSBitmapImageRep((_ data: NSData)

解説

ビットマップデータ(bitmapData)でNSBitmapImageRepを作って、初期化して返します。
解読できないビットマップデータならnilを返します。

返り値

Objective-C

instancetype

NSArray

Swift

[NSImageRep]

NSBitmapImageRepの配列

引数

( NSData * )bitmapData

ビットマップデータ

フレームワーク

ApplicationKit

クラス

NSBitmapImageRep

使用可能

10.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 dataWithContentsOfFile: [ opImage filename ] ];
        bmpRep = [NSBitmapImageRep imageRepWithData:dat];
        [img addRepresentation:bmpRep];
 
     
        NSLog([NSString stringWithFormat:@"bit / pixel:%d",[bmpRep bitsPerPixel]]);
    }
}

@end

Swift

    //NSBitmapImageRep +imageRepWithData:
    //NSBitmapImageRep
    //Swift2.0
    @IBAction func function003(sender: AnyObject) {
        
        //バンドル取得
        let nibBundle : NSBundle = NSBundle.mainBundle()
        
        //バンドルにある画像から
        let nibPath : NSString = nibBundle.pathForResource("testImage.JPG", ofType: nil )!
        
        //画像データを読み込み
        //NSLog("nibPath %@",nibPath )
        
        let theData : NSMutableData = NSMutableData(contentsOfFile: nibPath as String )!
        
        //オフスクリーンを作成
        let theBitmap : NSBitmapImageRep = NSBitmapImageRep(data: theData)!

        //ビットマップフォーマット
        switch (theBitmap.bitmapFormat){
        case NSBitmapFormat.NSAlphaFirstBitmapFormat :
            NSLog("NSAlphaFirstBitmapFormat ")
        case NSBitmapFormat.NSAlphaNonpremultipliedBitmapFormat :
            NSLog("NSAlphaNonpremultipliedBitmapFormat ")
        case NSBitmapFormat.NSFloatingPointSamplesBitmapFormat :
            NSLog("NSFloatingPointSamplesBitmapFormat ")
        case NSBitmapFormat.NS16BitLittleEndianBitmapFormat :
            NSLog("NS16BitLittleEndianBitmapFormat ")
        case NSBitmapFormat.NS32BitLittleEndianBitmapFormat :
            NSLog("NS32BitLittleEndianBitmapFormat ")
        case NSBitmapFormat.NS16BitBigEndianBitmapFormat :
            NSLog("NS16BitBigEndianBitmapFormat ")
        case NSBitmapFormat.NS32BitBigEndianBitmapFormat :
            NSLog("NS32BitBigEndianBitmapFormat ")
        default:
            NSLog("default")
        }
        
        //ピクセルあたりのビット数
        NSLog("bitsPerPixel = %d",theBitmap.bitsPerPixel)
       
        //Planeごとのバイト数
        NSLog("bytesPerPlane = %d",theBitmap.bytesPerPlane)

        //画像の1ラインのバイト数
        NSLog("bytesPerRow = %d",theBitmap.bytesPerRow)
        
        //プレーンの数
        NSLog("numberOfPlanes = %d",theBitmap.numberOfPlanes)

        //サンプル数
        NSLog("samplesPerPixel = %d",theBitmap.samplesPerPixel)
        
        //チャンネルあたりのバイト数
        NSLog("byte per pixel = %d",(theBitmap.bytesPerRow/theBitmap.samplesPerPixel) )

        
        
        //一度TIFFデータにしてみる
        let theData2 : NSData = theBitmap.TIFFRepresentation!
        var theData3 : NSMutableData = NSMutableData(data: theData2 )
        
        //データ書き換え
        let aLength : Int = 400000
        theData3.replaceBytesInRange(NSMakeRange(100, aLength), withBytes: &theData3, length: aLength)
        
        //imageRepWithData
        let theData4 : NSData = NSData(data: theData3 )
        let theBitmap2 : NSBitmapImageRep = NSBitmapImageRep(data: theData4)!
        
        
        //ビットマップの加工、ここではオレンジに塗りつぶす
        for y in 20...460 {
            for x in 20...620 {
                theBitmap2.setColor(NSColor.orangeColor(), atX: x, y: y)
            }
        }
        
        let destiPath : NSString = ("~/Desktop/test003.png" as NSString).stringByExpandingTildeInPath
        
        theBitmap2.representationUsingType(NSBitmapImageFileType.NSPNGFileType, properties: [:])?.writeToFile(destiPath as String, atomically: true)
        
        
    }

更新時バージョン

OS X 10.11