使用NSData从int*创建NSbitmapRep的NSimage?

Joe*_*Joe 1 macos cocoa objective-c

我有一个int*的rgb数据,宽度,高度和scanlinestride,我想创建一个NSImage.

我环顾四周,发现我需要使用NSData?

这样做的理想方式是什么?

Tod*_*ell 9

使用此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
Run Code Online (Sandbox Code Playgroud)

认真.它很容易使用:

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];
Run Code Online (Sandbox Code Playgroud)