2010-03-22 34 views

回答

8

使用此方法NSBitmapImageRep:

- (id)initWithBitmapDataPlanes:(unsigned char **)planes pixelsWide:(NSInteger)width pixelsHigh:(NSInteger)height bitsPerSample:(NSInteger)bps samplesPerPixel:(NSInteger)spp hasAlpha:(BOOL)alpha isPlanar:(BOOL)isPlanar colorSpaceName:(NSString *)colorSpaceName bitmapFormat:(NSBitmapFormat)bitmapFormat bytesPerRow:(NSInteger)rowBytes bitsPerPixel:(NSInteger)pixelBits 

重視。它很容易使用,雖然:

NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] 
    initWithBitmapDataPlanes:(unsigned char **)&bitmapArray 
    pixelsWide:width pixelsHigh:height 
    bitsPerSample:8 
    samplesPerPixel:3 // or 4 with alpha 
    hasAlpha:NO 
    isPlanar:NO 
    colorSpaceName:NSDeviceRGBColorSpace 
    bitmapFormat:0 
    bytesPerRow:0 // 0 == determine automatically 
    bitsPerPixel:0]; // 0 == determine automatically 

NSImage *image = [[NSImage alloc] initWithSize:NSMakeSize(width, height)]; 

[image addRepresentation:bitmap]; 
+0

(unsigned char **)&bitmapArray這是一個好的強制轉換如果bitmapArray是int \ * ?? – Joe

+0

雖然它真的是'int *'數據嗎?您需要使用'unsigned char *'來獲得每像素每通道8位。 –

+0

據我所知,NSBitmapImageRep只接受字符。如果你有整數,你可能需要先將它轉換爲char數組。我可能是錯的 - 我只是以圖像數據作爲字符。 –