yosh
yosh

Reputation: 3305

How to rotate image file?

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();

Upvotes: 2

Views: 1857

Answers (1)

memmons
memmons

Reputation: 40502

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();
}

Upvotes: 2

Related Questions