2014-01-31 30 views
2

我有一個無符號字符數組中的圖像的灰度值,我想將其轉換爲CGImage,以便我可以使用它通過UIImage在iOS中顯示它。無符號字符數組的每個值都是灰度圖像的像素值。我正在使用下面的代碼,但圖像不顯示。隱藏字節數組到CGImage

-(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位/像素; 1分量色彩空間; kCGImageAlphaNoneSkipLast; 100字節/行。 Jan 31 15:25:50 sumits-mbp.bsb.igloonet check_visibility_image [4210]: CGBitmapContextCreateImage:invalid context 0x0。這是一個嚴重的 錯誤。此應用程序或其使用的庫正在使用無效的上下文,從而有助於系統穩定性和可靠性的總體降級。此通知是禮貌性的:請 修復此問題。這將成爲即將到來的更新中的致命錯誤。

+0

你對圖像格式的數據代表了更多的細節?從錯誤消息中可以明顯看出,您提供的圖像數據不適合用於創建BitmapContext的參數。例如對於正常的灰度圖像,「kCGImageAlphaNoneSkipLast」可能是「kCGImageAlphaNone」。 – Volker

+0

謝謝@Volker,我得到了答案:) –

+0

如果它有效,我可能會發表評論作爲答案 – Volker

回答

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

}