在ImageView中旋转图像并保存到服务器

时间:2015-09-30 06:54:03

标签: ios imageview gesture

我们如何在图像视图中旋转图像并在图像上应用手势,并将该图像以与图像相同的状态保存到服务器

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

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

从这段代码中我能够将图像视图旋转90度

5 个答案:

答案 0 :(得分:7)

使用此: -

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

@implementation UIImage (RotationMethods)

static CGFloat getRadianFromDegree(CGFloat 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

答案 1 :(得分:1)

尝试这是最好的解决方案,我也使用它。

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

你可以这样称呼,

[self imageRotatedByDegrees:<your Image> deg:90];

答案 2 :(得分:0)

一旦您旋转了UIImageView,就可以使用类似

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

现在您可以将imageObject保存到您的服务器

答案 3 :(得分:0)

我认为最简单的方法(也是线程安全)是:

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

答案 4 :(得分:0)

试用此代码

Image.transform=CGAffineTransformMakeRotation(M_PI / 2);

self.imageview.image=Image;
相关问题