I recently was working on an application for the iPhone and wanted to capture images using the built-in camera. I wanted to use something a little more advanced/customizable than the default ImagePicker, so I utilized the AVCaptureSession as detailed in this blog post from musicalgeometry.

When it came to viewing the images I had captured in the session, I quickly found out that they needed to have their orientation fixed and they needed to be scaled/cropped correctly. I had set up a square rect to use in the AVCaptureSession preview, so I wanted my final image to be displayed as a square. This function takes a UIImage and returns a UIImage that is scaled to the specified size and has the correct orientation.

- (UIImage *)squareImageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
    double ratio;
    double delta;
    CGPoint offset;
    //make a new square size, that is the resized imaged width
    CGSize sz = CGSizeMake(newSize.width, newSize.width);
    //figure out if the picture is landscape or portrait, then
    //calculate scale factor and offset
    if (image.size.width > image.size.height) {
        ratio = newSize.width / image.size.width;
        delta = (ratio*image.size.width - ratio*image.size.height);
        offset = CGPointMake(delta/2, 0);
    } else {
        ratio = newSize.width / image.size.height;
        delta = (ratio*image.size.height - ratio*image.size.width);
        offset = CGPointMake(0, delta/2);
    //make the final clipping rect based on the calculated values
    CGRect clipRect = CGRectMake(-offset.x, -offset.y,
                                 (ratio * image.size.width) + delta,
                                 (ratio * image.size.height) + delta);
    //start a new context, with scale factor 0.0 so retina displays get
    //high quality image
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
        UIGraphicsBeginImageContextWithOptions(sz, YES, 0.0);
    } else {
    [image drawInRect:clipRect];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    return newImage;

This example is mostly taken from stackoverflow and other forum posts. While I found multiple solutions to this problem, I combined a few of them to make something that worked correctly on iOS 5+ and with the Retina screen. Some of the other solutions I found were for older versions of iOS or they made images look blurry on Retina devices.