2010-10-18 49 views
1

我是新编程的目标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; 

输出的肖像图像甚至没有一个正确的颜色。

回答

0

通过只是保持如果天气条件是横向模式或纵向模式,然后改变ImageView的框架

imageView.image=rawImage; 这里您要添加到ImageView的则只是改变的ImageView的框架在这​​两种情况下的要求。