Rotate Original Image by clicking button in Objective C
Create this UI
Step 2
--------------------------do the changes to ViewController.h class......................
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
NSData *imgData;
}
@property(nonatomic,strong)IBOutlet UIImageView *roateImageView;
@property(nonatomic,strong)IBOutlet UIImageView *showRoateImageView;
-(IBAction)rotateImageClick:(id)sender;
-(IBAction)leftRotateImageClick:(id)sender;
-(IBAction)showRotateImageClick:(id)sender;
@end
Step 3
--------------------------do the changes to ViewController.m class......................
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any
additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose
of any resources that can be recreated.
}
#pragma mark - IBAction method
-(IBAction)rotateImageClick:(id)sender{
UIImage *image2=[[UIImage alloc]init];
image2 = [self imageRotatedByDegrees:self.roateImageView.image deg:(90)]; //Angle by
90 degree
self.roateImageView.image = image2;
imgData= UIImageJPEGRepresentation(image2,0.9f);
}
-(IBAction)leftRotateImageClick:(id)sender{
UIImage *image2=[[UIImage alloc]init];
image2 = [self imageRotatedByDegrees:self.roateImageView.image deg:(-90)];//Angle by
-90 degree
self.roateImageView.image = image2;
imgData= UIImageJPEGRepresentation(image2,0.9f);
}
-(IBAction)showRotateImageClick:(id)sender{
UIImage* image = [UIImage imageWithData:imgData];
self.showRoateImageView.image = image;
}
#pragma mark - imageRotatedByDegrees Method
- (UIImage *)imageRotatedByDegrees:(UIImage*)oldImage
deg:(CGFloat)degrees{
// calculate
the size of the rotated view's containing box for our drawing space
UIView
*rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,oldImage.size.width, oldImage.size.height)];
CGAffineTransform t = CGAffineTransformMakeRotation(degrees * M_PI / 180);
rotatedViewBox.transform = t;
CGSize rotatedSize =
rotatedViewBox.frame.size;
// Create
the bitmap context
UIGraphicsBeginImageContext(rotatedSize);
CGContextRef bitmap = UIGraphicsGetCurrentContext();
// Move the
origin to the middle of the image so we will rotate and scale around the
center.
CGContextTranslateCTM(bitmap,
rotatedSize.width/2, rotatedSize.height/2);
// // Rotate the image context
CGContextRotateCTM(bitmap,
(degrees * M_PI / 180));
// Now, draw
the rotated/scaled image into the context
CGContextScaleCTM(bitmap, 1.0, -1.0);
CGContextDrawImage(bitmap, CGRectMake(-oldImage.size.width / 2, -oldImage.size.height / 2, oldImage.size.width, oldImage.size.height), [oldImage CGImage]);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
@end
Step 4
set ur image to roateImageView and connect IBOutlet and IBAction