通过将图像加载为cgImage而不是uiImage,使用cgImageCreateWithPngDataProvider(),您可能能够获得索引颜色空间。见:
http://developer.apple.com/iphone/library/documentation/GraphicsImaging/Reference/CGColorSpace/Reference/reference.html
其中列出了cgcolorspaceCreateIndexed()、cgcolorspaceGetColorTable()等。使用cgColorSpaceGetModel(cgImageGetColorSpace(img))查看最终使用的颜色空间是否为索引空间,然后使用cgImageGetDataProvider()获取cgDataProviderRef,可以使用cgDataProviderPyData()获取实际位图数据…
编辑
赏金总能使事情顺利进行。我做了测试,结果很好。(不好意思处理得很糟糕,这当然是概念证明)
NSString *path = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:@"test.png"];
printf("path: %s\n",[path UTF8String]);
NSData *file = [[NSFileManager defaultManager] contentsAtPath:path];
if ( !file ) printf("file failed\n");
CGDataProviderRef src = CGDataProviderCreateWithCFData(file);
if ( !src ) printf("image failed\n");
CGImageRef img = CGImageCreateWithPNGDataProvider(src, NULL, NO, kCGRenderingIntentDefault);
if ( !img ) printf("image failed\n");
printf("Color space model: %d, indexed=%d\n",
CGColorSpaceGetModel(CGImageGetColorSpace(img)),
kCGColorSpaceModelIndexed);
输出:
path: /Users/..../638...8C12/test.app/test.png
Color space model: 5, indexed=5
QED?
ps.我的测试图像来自libgd,通过php,使用
$img = imagecreatefrompng("whateverimage.png");
imagetruecolortopalette($img,false,256);
header("Content-Type: image/png");
imagepng($img);
结果在我的案例中(b/w图像)
$ file test.png
test.png: PNG image, 2000 x 300, 1-bit colormap, non-interlaced
编辑^ 2
这是访问位图数据的方式。ASCII艺术FTW!
CGDataProviderRef data = CGImageGetDataProvider(img);
NSData *nsdata = (NSData *)(CGDataProviderCopyData(data));
char *rawbuf = malloc([nsdata length]);
if ( !rawbuf ) printf("rawbuf failed\n");
[nsdata getBytes:rawbuf];
int w = CGImageGetWidth(img);
int h = CGImageGetHeight(img);
int bpl = CGImageGetBytesPerRow(img);
printf("width: %d (%d bpl), height: %d, pixels: %d, bytes: %d\n",w,bpl,h,bpl*h,[nsdata length]);
if ( [nsdata length] != bpl*h )
{
printf("%d pixels is not %d bytes, i may be crashing now...\n",bpl*h,[nsdata length]);
}
for ( int y=0;y<h; y++ )
{
for ( int x=0;x<w; x++ )
{
char c = rawbuf[y*bpl+x];
while ( !isalnum(c) ) c += 31; //whoa!
printf("%c",c);
}
printf("\n");
}