2014-02-20 4 views
0

PNG로 저장하고 싶지만 알파 채널을 제거하고 5 비트 색상을 사용하려는 NSI 이미지가 있습니다. 나는 현재이 내 PNG를 만드는 중이 야 :알파 및 5 비트 색상이없는 PNG로 NSImage 저장

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을 직접 만들 수 있습니까? 코드 스 니펫 (snippet)을 사용하면 도움이됩니다.

건배

데이브

답변

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); 
관련 문제