Saturday, July 18, 2009

iPhone OS - drawing image and stupid thing from Apple

Just because of a piece of code, a team in Singapore gave up an iPhone project. Actually, they do not know how to play tricks with Apple. ... and I also faced to a similar situation when I play as a technical consultant to fix this stupid bug.

The need of client is: drawing some effects on an image and save it as a photo in Photo library. So, if anyone has experience with graphic programming may think this is a very simple task. However, that's not true.

Many forums and online articles just shows us a real problem on iPhone graphics programming: the root position (0,0) of the image to draw is on the bottom left and the root position of the graphic context is on the top-left corner. So, we must do some transformation to keep the image in the correct position.

And the simple code to fix this issue is as below:
int width = image.size.width;
int height = image.size.height;
CGSize size = CGSizeMake(width, height);
//create the rect zone that we draw from the image
CGRect imageRect = CGRectMake(0, 0, width, height);
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
//Save current status of graphics context
CGContextSaveGState(context);
//Do stupid stuff to draw the image correctly
CGContextTranslateCTM(context, 0, height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, imageRect, image.CGImage);
//After drawing the image, roll back all transformation by restoring the 
//old context
CGContextRestoreGState(context);
DO OTHER EFFECTS HERE
//get the image from the graphic context
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
//commit all drawing effects
UIGraphicsEndImageContext();
Above piece of code is thing that you see in all online technical articles related to iPhone graphic programming. Anyway, that's not enough. If you deploy that piece of code into iPhone OS 3.0 on real device, it does not run correctly: saved images are always scaled in horizontal and inverse. However, there is a very strange thing: this code is run perfectly on simulator for iPhone OS 3.0.

After 1 day to detect the problem, I found that: when an image is shown on iPhone, it has a direction. Direction is included: UP, DOWN, LEFT, RIGHT. So, we must to fix the code to satisfy when they're in those cases. The above code is run correctly in case image direction is UP.

Here is the fixed version:
int width = image.size.width;
int height = image.size.height;
CGSize size = CGSizeMake(width, height);
//create the rect zone that we draw from the image
CGRect imageRect;

if(image.imageOrientation==UIImageOrientationUp 
|| image.imageOrientation==UIImageOrientationDown) 
{
    imageRect = CGRectMake(0, 0, width, height); 
}
else 
{
    imageRect = CGRectMake(0, 0, height, width); 
}

UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
//Save current status of graphics context
CGContextSaveGState(context);

//Do stupid stuff to draw the image correctly
CGContextTranslateCTM(context, 0, height);
CGContextScaleCTM(context, 1.0, -1.0);

if(image.imageOrientation==UIImageOrientationLeft) 
{
    CGContextRotateCTM(context, M_PI / 2);
    CGContextTranslateCTM(context, 0, -width);
}
else if(image.imageOrientation==UIImageOrientationRight) 
{
    CGContextRotateCTM(context, - M_PI / 2);
    CGContextTranslateCTM(context, -height, 0);
} 
else if(image.imageOrientation==UIImageOrientationUp) 
{

//DO NOTHING

}
else if(image.imageOrientation==UIImageOrientationDown) 
{
    CGContextTranslateCTM(context, width, height);
    CGContextRotateCTM(context, M_PI);
}

CGContextDrawImage(context, imageRect, image.CGImage);
//After drawing the image, roll back all transformation by restoring the 
//old context
CGContextRestoreGState(context);
DO OTHER EFFECTS HERE
//get the image from the graphic context
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
//commit all drawing effects
UIGraphicsEndImageContext();
After fixing this bug, I can say only one thing: why Apple always do something stupid and make developer do the stupid thing?