0

PNG として保存したい NSImage がありますが、アルファ チャネルを削除して 5 ビット カラーを使用します。現在、PNGを作成するためにこれを行っています:

NSData *imageData = [image TIFFRepresentation];
NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData];

NSDictionary *imageProps = nil;
imageData = [imageRep representationUsingType:NSPNGFileType properties:imageProps];

[imageData writeToFile:fileNameWithExtension atomically:YES];

SOに関する同様の質問をたくさん読んだことがありますが、使用するための最良/正しいアプローチについて混乱しています。新しい CGGraphics コンテキストを作成し、そこに描画しますか? これらのパラメーターを使用して新しい imageRep を直接作成できますか? コードスニペットを使用して、どんな助けでも大歓迎です。

乾杯

デイブ

4

1 に答える 1

0

私は最終的にこれをしました。醜く見え、私にはにおいがします。より良い提案があれば大歓迎です。

// Create a graphics context (5 bits per colour, no-alpha) to render the tile
static int const kNumberOfBitsPerColour = 5;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef tileGraphicsContext = CGBitmapContextCreate (NULL, rect.size.width, rect.size.height, kNumberOfBitsPerColour, 2 * rect.size.width, colorSpace, kCGBitmapByteOrder16Little | kCGImageAlphaNoneSkipFirst);

// Draw the clipped part of the image into the tile graphics context
NSData *imageData = [clippedNSImage TIFFRepresentation];
CGImageRef imageRef = [[NSBitmapImageRep imageRepWithData:imageData] CGImage];
CGContextDrawImage(tileGraphicsContext, rect, imageRef);

// Create an NSImage from the tile graphics context
CGImageRef newImage = CGBitmapContextCreateImage(tileGraphicsContext);
NSImage *newNSImage = [[NSImage alloc] initWithCGImage:newImage size:rect.size];

// Clean up
CGImageRelease(newImage);
CGContextRelease(tileGraphicsContext);
CGColorSpaceRelease(colorSpace);
于 2014-02-21T14:19:23.417 に答える