我想从 Pix 获取 UIImage 而不对文件进行任何读/写操作。我收到 CGBitmapContextCreate 错误。
这是 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.