-2

I have an iPad app where I'm using the camera. The original image is 480 x 640. I am attempting to resize it to 124 x 160 and then store it in CoreData using this code that I found on the internet:

- (UIImage *)resizeImage:(UIImage*)image newSize:(CGSize)newSize {

CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
CGImageRef imageRef = image.CGImage;

UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();

// Set the quality level to use when rescaling
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height);

CGContextConcatCTM(context, flipVertical);

// Draw into the context; this scales the image
CGContextDrawImage(context, newRect, imageRef);

// Get the resized image from the context and a UIImage
CGImageRef newImageRef = CGBitmapContextCreateImage(context);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];

CGImageRelease(newImageRef);
UIGraphicsEndImageContext();

return newImage;

}

The image is returned to me rotated counter-clockwise 90 degrees and I don't see why. I have tried commenting out this statement:

CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height);

but it makes no difference. What is wrong here?

SpokaneDude
  • 4,856
  • 13
  • 64
  • 120
  • 1
    Isn't the rotation of an image a property of the meta data and not an actual realignment of the pixels? At least that's how I think it works with pictures captured with the iPod/iPhone/iPad's cameras (as well as many other consumer cameras) – Jason Sperske Apr 23 '13 at 18:23
  • 3
    He who lives by the copy-and-pasted code snippet dies by the copy-and-pasted code snippet. The thing to do here is think. UIImage has orientation information. CGImage does not. Hence you are throwing that information away and never restoring it. – matt Apr 23 '13 at 18:32
  • OK... I've tried everything suggested here, and nothing helps... I understand what matt is saying, but that doesn't help... all I wanted to do was save space by scaling the image... seems like this is a lot of work for little payoff... – SpokaneDude Apr 23 '13 at 19:31
  • Is the orientation of the UIImage set, and if so what is it set to? – Wain Apr 23 '13 at 19:37
  • no, it's the raw UIImage taken with the iPad camera in portrait mode. – SpokaneDude Apr 23 '13 at 20:01

2 Answers2

0

Thanks to everybody who made suggestions.. I kept looking and found this, which scales and keeps the orientation:

    CGRect screenRect = CGRectMake(0, 0, 120.0, 160.0);
    UIGraphicsBeginImageContext(screenRect.size);
    [image drawInRect:screenRect blendMode:kCGBlendModePlusDarker alpha:1];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
SpokaneDude
  • 4,856
  • 13
  • 64
  • 120
-1

You need to consider the orientation of the image when drawing it like this. Check this answer iOS - UIImageView - how to handle UIImage image orientation

Community
  • 1
  • 1
Wain
  • 118,658
  • 15
  • 128
  • 151