ios - 将 Byte 数组转换为 CGImage

标签 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: unsupported parameter combination: 8 integer bits/component; 16 bits/pixel; 1-component color space; kCGImageAlphaNoneSkipLast; 100 bytes/row. Jan 31 15:25:50 sumits-mbp.bsb.igloonet check_visibility_image[4210] : CGBitmapContextCreateImage: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.

最佳答案

这段代码现在可以工作了,我遇到了问题。

- (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);

}

关于ios - 将 Byte 数组转换为 CGImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21477402/

相关文章:

ios - 属性(property)观察员 (KVO) : are they still the way to go?

iOS 为应用程序内容购买本地货币

ios - Autorotation 仅适用于模拟器而不适用于设备(​​IOS 7.1)

ios - UITableView 在半透明的导航栏下

ios - 在 AFNetworking 的 JSONRequestOperationWithRequest :success:failure: method, 中,JSON 响应是什么类型的对象?

ios - 使用 NSPredicate 获取给定键的所有值

ios - 如何创建一个导航栏,在快速滚动后固定在顶部

ios - iCloud 数据复制

ios - 在 viewDidLoad 中调用 addSubView 时的奇怪行为

ios - 单元目标测试构建失败 xcode objective-c