2

how can we rotate a image in image view and apply gesture on image and save that image to server at same state as image is

- (IBAction)imageMove:(id)sender {
    static int numRot = 0;

    myimage.transform = CGAffineTransformMakeRotation(M_PI_2 * numRot);
    ++numRot;
}

from this piece of code i am able to rotate my image view 90 degree

Akshay Mehta
  • 213
  • 1
  • 8

5 Answers5

7

Use This:-

@interface UIImage (RotationMethods)
- (UIImage *)rotateImageByDegree:(CGFloat)degrees;
@end

@implementation UIImage (RotationMethods)

static CGFloat getRadianFromDegree(CGFloat degrees) 

{return degrees * M_PI / 180;};

- (UIImage *) rotateImageByDegree:(CGFloat)degrees 
{   
    UIView *rotatedImageView = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.size.width, self.size.height)];
    CGAffineTransform t = CGAffineTransformMakeRotation(getRadianFromDegree(degrees));
    rotatedImageView.transform = t;
    CGSize rotatedSize = rotatedImageView.frame.size;
    UIGraphicsBeginImageContext(rotatedSize);
    CGContextRef bitmap = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);
    CGContextRotateCTM(bitmap, getRadianFromDegree(degrees));
    CGContextScaleCTM(bitmap, 1.0, -1.0);
    CGContextDrawImage(bitmap, CGRectMake(-self.size.width / 2, -self.size.height / 2, self.size.width, self.size.height), [self CGImage]);
    UIImage *rotatedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return rotatedImage;

}

@end
Ashutosh Maurya
  • 341
  • 1
  • 6
1

try this this is the best solution and i have use this also.

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

and you can call this like,

[self imageRotatedByDegrees:<your Image> deg:90];
JAY RAPARKA
  • 1,353
  • 2
  • 13
  • 33
0

Once you've rotated the the UIImageView, you can get the UIImage using something like

CGRect imageViewRect = [imageView bounds];
UIGraphicsBeginImageContext(imageViewRect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[imageView.layer renderInContext:context];   
UIImage *imageObject = UIGraphicsGetImageFromCurrentImageContext();

Now you can save the imageObject to your server

Shamas S
  • 7,507
  • 10
  • 46
  • 58
0

I believe the easiest way (and thread safe too) is to do:

 UIImage * LandscapeImage = [UIImage imageNamed: imgname];
    UIImage * PortraitImage = [[UIImage alloc] initWithCGImage: LandscapeImage.CGImage
                                                         scale: 1.0
                                                   orientation: UIImageOrientationRight];
0

Try This Code

Image.transform=CGAffineTransformMakeRotation(M_PI / 2);

self.imageview.image=Image;
Rohit suvagiya
  • 1,005
  • 2
  • 12
  • 40