我不太了解预乘alpha。是否有可能使24 bpp和没有alpha通道的NSBitmapImageRep?
我需要一个没有alpha通道的NSBitmapImageRep(我不需要特定的bpp)。
我的问题是,这个代码给我的错误:
NSSize imageSize = NSMakeSize(200, 200);
//create a non-alpha RGB image rep with the same dimensions as the image
NSBitmapImageRep* bitmap = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:NULL
pixelsWide:imageSize.width
pixelsHigh:imageSize.height
bitsPerSample:8
samplesPerPixel:3
hasAlpha:NO
isPlanar:NO
bitmapFormat:NSAlphaNonpremultipliedBitmapFormat
bytesPerRow:(3 * imageSize.width)
bitsPerPixel:24];
//lock focus on the bitmap
NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithBitmapImageRep:bitmap];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:context];
//draw the image into the bitmap
[prueba drawAtPoint:NSMakePoint(0, 0) withAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[NSFont systemFontOfSize:24], NSFontAttributeName, nil]];
[NSGraphicsContext restoreGraphicsState];
//get the TIFF data
NSData* tiffData = [bitmap TIFFRepresentation];
//do something with TIFF data
NSError *error = nil;
[tiffData writeToFile:@"/Users/Paul/test.tif" options:NSDataWritingAtomic error:&error];
Error: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 24 bits/pixel; 3-component color space; kCGImageAlphaNone; 640 bytes/row.
好吧,我知道这个组合它不支持,但我需要的是这样的,我没有找到一个解决方案。
您使用'saveGraphicsState'和'restoreGraphicsState'没有意义:您可以保存在一个上下文中,然后在不同的上下文中恢复,而不必先前保存在上下文中。图形状态是每个上下文的,而不是其他方式。 – 2012-01-02 20:36:48