2

How can I easily rotate image file saved in documents directory?

I'm saving it with code:

(...)
UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();
NSData* imageData = UIImageJPEGRepresentation(capturedImage, 1.0);
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
[imageData writeToFile:[docDir stringByAppendingPathComponent:@"saved.jpg"] atomically:NO];
UIGraphicsEndImageContext();
yosh
  • 3,245
  • 7
  • 55
  • 84

1 Answers1

2

Easily accomplished using CGContextRotateCTM():

Found in a SO answer here: How to Rotate a UIImage 90 degrees?

static inline double radians (double degrees) {return degrees * M_PI/180;}
- (UIImage*) rotateImage:(UIImage*)src rotationDirection:(UIImageOrientation)orientation {
    UIGraphicsBeginImageContext(src.size);

    CGContextRef context(UIGraphicsGetCurrentContext());
    if (orientation == UIImageOrientationRight) {
        CGContextRotateCTM (context, radians(90));
    } else if (orientation == UIImageOrientationLeft) {
        CGContextRotateCTM (context, radians(-90));
    } else if (orientation == UIImageOrientationDown) {
        // NOTHING
    } else if (orientation == UIImageOrientationUp) {
        CGContextRotateCTM (context, radians(90));
    }

    [src drawAtPoint:CGPointMake(0, 0)];

    return UIGraphicsGetImageFromCurrentImageContext();
}
Community
  • 1
  • 1
memmons
  • 40,222
  • 21
  • 149
  • 183
  • This isn't even really a Obj-C method.. I tried making something out of it but it didn't rotate anything in this case. – yosh May 20 '11 at 09:33
  • It's an easily callable function. But, if you prefer a method, no problem. I've changed the code to reflect that. – memmons May 22 '11 at 14:59
  • Sorry, I was checking similar function code before and had multiple syntax errors. I tried CGContextRotateCTM before but didn't do anything, will try your solution in a bit, thanks! – yosh May 23 '11 at 08:55