Make sure to test every code path

My story is not as dramatic as some, but I thought it was amusing. I’ve been using my PDF class for quite a while now, and it’s seemed to work perfectly so far in shipping code. But while working on a new app suddenly a tall skinny image was rendering too tall. When I fixed that, suddenly the image was off center. What was wrong?

Well, the PDFView class is designed to render it’s PDF’s as large as possible while preserving the aspect ratio. This means it takes the entire space in either the x- or y- direction, and centers itself in the other direction. But out of 913 images, apparently none of them filled the y-direction so that centering code had never been tested. So the code was failing to actually center properly.

It was a simple fix. Here’s the few lines to change to make the drawRect code work correctly:

 
        if (xRatio < yRatio) {
            ratio = xRatio;
            CGContextTranslateCTM(context, 0.0, -(self.bounds.size.height-boxRect.size.height*ratio)/2);
        } else {
            ratio = yRatio;
            CGContextTranslateCTM(context, (self.bounds.size.width-boxRect.size.width*ratio)/2, 0.0);
        }
Advertisements

Leave a comment

Filed under Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s