gpt4 book ai didi

ios - 将 Byte 数组转换为 CGImage

转载 作者:行者123 更新时间:2023-12-01 16:44:12 25 4
gpt4 key购买 nike

我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com