2010-10-18 51 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的框架在這​​兩種情況下的要求。