views:

27

answers:

0

Hello Developers, I 'm new to programming with objective C. I try to read the argb data from an image and then display this image. It works for landscape images, but doesn't for portrait images. Here is the code I use.

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

The output portrait image has not even one correct colour.

Thanks in advance for all your help