将Byte数组转换为UIImage

时间:2014-01-31 10:12:10

标签: ios objective-c

我在unsigned char数组中有一个图像的灰度值,我想将其转换为CGImage,以便我可以使用它通过UIImage在iOS中显示它。 unsigned char数组的每个值都是灰度图像的像素值。我使用以下代码但不显示图像。

-(void)viewDidLoad
{
[super viewDidLoad];
NSString *filename = [[NSBundle mainBundle] pathForResource:@"rectange4obs1pro" ofType:@"pgm"];
NSFileManager *fm = [NSFileManager defaultManager];
/////////////// read file ///////////
FILE *file = fopen([filename UTF8String], "rb");
if (file == NULL) {
    NSLog(@"File Not Found");
    return;
}
char name[20], secondline[10];
int w=0, h=0, colors=0;
fscanf(file, "%s", name);
fscanf(file, "%s", secondline);
fscanf(file, "%d %d %d", &w, &h, &colors);
printf("%d %d\n", w, h);
unsigned char * imagedata = (unsigned char *) malloc(sizeof(unsigned char)*w*h);
fseek(file, 1, SEEK_CUR);
int i=0;
while (!feof(file)) {
    imagedata[i] = getc(file);
    i++;
}
////////////////// end read file /////////////

CGColorSpaceRef colorSpace= CGColorSpaceCreateDeviceGray();
CGContextRef bitmapContext=CGBitmapContextCreate(imagedata, w, h, 8, w, colorSpace,  kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
CFRelease(colorSpace);
free(imagedata);
CGImageRef cgImage=CGBitmapContextCreateImage(bitmapContext);
CGContextRelease(bitmapContext);

UIImage * newimage = [UIImage imageWithCGImage:cgImage];
CGImageRelease(cgImage);
[imageview setImage:newimage];
}

我在终端上收到的消息是:

  

CGBitmapContextCreate:不支持的参数组合:8整数   位/分量; 16位/像素;单组分色彩空间;   kCGImageAlphaNoneSkipLast; 100字节/行。 1月31日15:25:50   sumits-mbp.bsb.igloonet check_visibility_image [4210]:   CGBitmapContextCreateImage:无效的上下文0x0。这是一个严肃的问题   错误。此应用程序或其使用的库使用无效   上下文,从而导致整体退化   系统稳定性和可靠性。这个通知是礼貌的:拜托   解决这个问题。在即将到来的更新中,它将成为一个致命的错误。

1 个答案:

答案 0 :(得分:0)

此代码现在正在运行,我遇到了问题。

- (void)viewDidLoad
{
[super viewDidLoad];
NSString *filename = [[NSBundle mainBundle] pathForResource:@"10CamsHGallery" ofType:@"pgm"];
NSFileManager *fm = [NSFileManager defaultManager];
/////////////// read file ///////////
FILE *file = fopen([filename UTF8String], "rb");
if (file == NULL) {
    NSLog(@"File Not Found");
    return;
}
char name[20], secondline[10];
int w=0, h=0, colors=0;
fscanf(file, "%s", name);
fscanf(file, "%s", secondline);
fscanf(file, "%d %d %d", &w, &h, &colors);
//printf("%d %d\n", w, h);
unsigned char * imagedata = (unsigned char *) malloc(sizeof(unsigned char)*w*h);
fseek(file, 1, SEEK_CUR);
int i=0;
while (!feof(file)) {
    int val = getc(file);
    imagedata[i] = val;
    i++;
}
////////////////// end read file /////////////

CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();
CFDataRef rgbData = CFDataCreate(NULL, imagedata, w * h);
CGDataProviderRef provider = CGDataProviderCreateWithCFData(rgbData);
CGImageRef rgbImageRef = CGImageCreate(w, h, 8, 8, w , colorspace, kCGBitmapByteOrderDefault, provider, NULL, true, kCGRenderingIntentDefault);
CFRelease(rgbData);
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorspace);
UIImage * newimage = [UIImage imageWithCGImage:rgbImageRef];
imageview.frame = CGRectMake(imageview.frame.origin.x,imageview.frame.origin.y, w, h);
[imageview setImage:newimage];
CGImageRelease(rgbImageRef);

}