Search code examples
iosimageviewgesture

Rotate Image in ImageView and save to server


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


Solution

  • 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