我是使用目标 C 编程的新手。我尝试从图像中读取 argb 数据,然后显示该图像。它适用于横向图像,但不适用于纵向图像。这是我使用的代码。
//Read the data
CGImageRef inImage = dispimage.CGImage;
CGColorSpaceRef colorspace = CGImageGetColorSpace(inImage);
CGContextRef cgctx = [self createARGBBitmapContextFromImage:inImage];
if (cgctx == NULL) { return nil; /* error */ }
size_t width = CGImageGetWidth(inImage);
size_t height = CGImageGetHeight(inImage);
CGRect rect = {{0,0},{width,height}};
CGContextDrawImage(cgctx, rect, inImage);
unsigned char* data = CGBitmapContextGetData (cgctx);
unsigned char alpha;
unsigned char R [height][width];
unsigned char G [height][width];
unsigned char B [height][width];
CGContextRelease(cgctx);
int length=4*width*height;
unsigned char result[length];
int i,j;
int kk=0;
for (i=0; i<height; i++) {
for (j=0; j<width; j++) {
result[kk]=alpha;
result[kk+1]=R[i][j];
result[kk+2]=G[i][j];
result[kk+3]=B[i][j];
kk=kk+4;
}
}
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
if (colorSpace == NULL) {
NSLog(@"Error allocating color space\n");
return NULL;
}
CGContextRef context =CGBitmapContextCreate(result, width, height, 8, width * 4, colorSpace, kCGImageAlphaPremultipliedFirst);
CGColorSpaceRelease(colorSpace);
CGImageRef imageRef = CGBitmapContextCreateImage (context);
UIImage *rawImage = [[UIImage imageWithCGImage:imageRef]retain];
imageView.image=rawImage;
输出的肖像图像甚至没有一种正确的颜色。