New*_*ack 10 macos cocoa nsimage nsbitmapimagerep
我用以下代码创建了32位NSImage.
NSBitmapImageRep *sourceRep = [[NSBitmapImageRep alloc] initWithData: imageData];
// create a new bitmap representation scaled down
NSBitmapImageRep *newRep =
[[NSBitmapImageRep alloc]
initWithBitmapDataPlanes: NULL
pixelsWide: imageSize
pixelsHigh: imageSize
bitsPerSample: 8
samplesPerPixel: 4
hasAlpha: YES
isPlanar: NO
colorSpaceName: NSCalibratedRGBColorSpace
bytesPerRow: 0
bitsPerPixel: 0];
// save the graphics context, create a bitmap context and set it as current
[NSGraphicsContext saveGraphicsState] ;
NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithBitmapImageRep: newRep];
[NSGraphicsContext setCurrentContext: context] ;
// draw the bitmap image representation in it and restore the context
[sourceRep drawInRect: NSMakeRect(0.0f, 0.0f, imageSize, imageSize)] ;
[NSGraphicsContext restoreGraphicsState] ;
// set the size of the new bitmap representation
[newRep setSize: NSMakeSize(imageSize,imageSize)] ;
NSDictionary *imageProps2 = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat:1.0], kCGImageDestinationLossyCompressionQuality,
nil];
imageData = [newRep representationUsingType: NSPNGFileType properties: imageProps2];
NSImage *bitImage = [[NSImage alloc]initWithData:imageData];
Run Code Online (Sandbox Code Playgroud)
现在我需要创建8位(256色),4位(16色),1位(黑白)NSBitmapImageRep表示.我现在想做什么?
可悲的是,我相信你不能使用核心显卡。图形上下文不支持任何带有这几个位的东西。
该文档有一个支持的像素格式表。
显然 Carbon 已经(有?)支持它,正如这里所引用的,他们也感叹 Cocoa 缺乏对它的支持:
事实证明,Cocoa/Quartz 基本上不支持将图像下采样到 8 位颜色。它支持绘制它们,并且支持上采样,但不支持相反的方式。我想这是 Apple 故意设计的,目的是摆脱将索引图像作为标准图形数据类型的做法 - 毕竟 32 位颜色要简单得多,对吧?是的,但是 8 位仍然有有用的用途。那么该怎么办?一种可能性是使用 Carbon,因为 General/QuickDraw 的 General/GWorld 确实支持下采样等。
从这个线程