0

I have the following code:

    float photoWidth = photo.size.width;
    float photoHeight = photo.size.height;

    UIImage *picture = nil;

    if (photoWidth < photoHeight)
    {
        // Portrait

        // Scale the photo down
        CGRect rect = CGRectMake(0.0, 0.0, 450.0, 600.0);
        UIGraphicsBeginImageContext( rect.size );
        [photo drawInRect:rect];
        picture = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
    else
    {
        // Landscape

        // Crop off the edges
        float scale = photoHeight / photoWidth;

        float newWidth = photoHeight * scale;
        float newHeight = photoHeight;
        float newX = (newHeight - newWidth) / 2.0;
        float newY = 0.0;

        CGRect cropped = CGRectMake(newX, newY, newWidth, newHeight);

        CGImageRef imageRef = CGImageCreateWithImageInRect ([photo CGImage], cropped);
        UIImage * croppedPhoto = [UIImage imageWithCGImage: imageRef];
        CGImageRelease (imageRef);

        // Scale the photo down
        CGRect rect = CGRectMake(0.0, 0.0, 450.0, 600.0);
        UIGraphicsBeginImageContext( rect.size );
        [croppedPhoto drawInRect:rect];
        picture = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }

    NSData *photoData = UIImagePNGRepresentation(picture);

When I use the photoData and turn it into a UIImage, the portrait mode works fine. But having issues with the landscape mode.

I am trying to make the landscape into the same size as my portrait by cropping off a left and right edge of the photo.

Also noticed that orientation makes my photo upsidedown if I have the landscape camera flipped when I take a picture.

Am I doing this the wrong way?

Thanks.

cdub
  • 24,555
  • 57
  • 174
  • 303

1 Answers1

0

Try to change

[UIImage imageWithCGImage: imageRef];

to

[UIImage imageWithCGImage:imageRef scale: photo.scale orientation:UIImageOrientationUp];

or

[UIImage imageWithCGImage:imageRef scale: photo.scale orientation:photo.imageOrientation];
Avt
  • 16,927
  • 4
  • 52
  • 72
  • I still can't get it too work. Depending on how I hold the landscape camera it's still upside down and the second rect is off. – cdub Jul 15 '14 at 05:39