创建UIImage时出现iOS:CGBitmapContext创建错误
我想从Pix中获取UIImage,但没有对文件操作进行任何读/写操作。我得到一个CGBitmapContextCreate错误。创建UIImage时出现iOS:CGBitmapContext创建错误
这里的Pix的结构:
struct Pix
{
l_uint32 w; /* width in pixels */
l_uint32 h; /* height in pixels */
l_uint32 d; /* depth in bits */
l_uint32 wpl; /* 32-bit words/line */
l_uint32 refcount; /* reference count (1 if no clones) */
l_int32 xres; /* image res (ppi) in x direction */
/* (use 0 if unknown) */
l_int32 yres; /* image res (ppi) in y direction */
/* (use 0 if unknown) */
l_int32 informat; /* input file format, IFF_* */
char *text; /* text string associated with pix */
struct PixColormap *colormap; /* colormap (may be null) */
l_uint32 *data; /* the image data */
};
typedef struct Pix PIX;
完整的文档可以在这里找到:http://tpgit.github.com/Leptonica/pix_8h_source.html
这里是我的尝试吧:
- (UIImage *) getImageFromPix:(PIX *) thePix
{
CGColorSpaceRef colorSpace;
uint32_t *bitmapData;
size_t bitsPerComponent = thePix->d;
size_t width = thePix->w;
size_t height = thePix->h;
size_t bytesPerRow = thePix->wpl * 4;
colorSpace = CGColorSpaceCreateDeviceGray();
if(!colorSpace) {
NSLog(@"Error allocating color space Gray\n");
return NULL;
}
// Allocate memory for image data
bitmapData = thePix->data;
if(!bitmapData) {
NSLog(@"Error allocating memory for bitmap\n");
CGColorSpaceRelease(colorSpace);
return NULL;
}
//Create bitmap context
CGContextRef context = CGBitmapContextCreate(bitmapData,
width,
height,
bitsPerComponent,
bytesPerRow,
colorSpace,
kCGImageAlphaNone);
if(!context) {
free(bitmapData);
NSLog(@"Bitmap context not created");
}
CGColorSpaceRelease(colorSpace);
CGImageRef imgRef = CGBitmapContextCreateImage(context);
UIImage * result = [UIImage imageWithCGImage:imgRef];
CGImageRelease(imgRef);
CGContextRelease(context);
return result;
}
下面是我收到的错误:
<Error>: CGBitmapContextCreate: unsupported parameter combination: 1 integer bits/component; 1 bits/pixel; 1-component color space; kCGImageAlphaNone; 16 bytes/row.
CGBitmapContextCreate()
不支持像素格式的每种组合。可能有许多不支持的Pix格式。支持的格式的完整列表可以在Quartz 2D Programming Guide中找到。 iOS支持的格式相当有限。您可能需要将您从Pix获得的内容转换为支持的格式之一。它很难做到这一般,但对于你的具体情况,如果你总是得到1位/像素,1位/分量,没有 alpha通道,它不应该太难转换为灰度格式的8位/像素,8位/组件,没有iOS支持的Alpha通道。
我该如何将其转换为灰度格式? – SuperString 2012-08-07 22:40:56
1 bpp,1 bpc,无alpha表示每个位代表一个像素值,或者打开或关闭。 8 bpp,8 bpc,没有字母表示每个字节代表0到255之间的灰色像素值。因此,您翻译的位图将比原始字体大8倍。您需要遍历原始位图的每一位,如果设置了,则将255写入转换的位图的相应字节,否则为0。 – 2012-08-08 00:17:53
右键 - 你想绘制3种颜色和alpha,但你指定了一个灰色的颜色空间(即一个“颜色”)。尝试使用CGColorSpaceCreateDeviceRGB()。哦,你也需要阿尔法,因为你在PIX里有alpha。你不需要alpha,但是稍后当你画iOS时会让它成为你的alpha - 占用空间和CPU - 所以最好现在就做。 – 2012-08-03 23:06:40
我其实只是想要一种颜色。我怎样才能做到这一点? – SuperString 2012-08-06 14:30:34