如何将UIImage和图像数据旋转90度 [英] How Can I Rotate A UIImage and Image Data By 90 Degrees

查看:127
本文介绍了如何将UIImage和图像数据旋转90度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含图像的UIImageView。用户可以点击将UIImageView中的图像保存到磁盘。

I have a UIImageView that contains an image. At the minute the user can click to save the image within the UIImageView to disk.

我想这样做,以便用户可以点击旋转UIImageView并在视图中旋转UImage,这样当图像保存时它会保持新的轮换。

I would like to make it so that the the user can click to rotate the UIImageView and also rotate the UImage within the view so that when the image is saved it keeps the new rotation.

我有的时间

- (IBAction)rotateImage:(id)sender {

float degrees = 90; //the value in degrees
self.imagePreview.transform = CGAffineTransformMakeRotation(degrees * M_PI/180);

}

有人能指出我关于保持现状的正确方向旋转。

Can someone point me in the right direction regarding keeping the current rotation.

谢谢

推荐答案

我的旧代码应用程序。但是这个代码可以简化,因为你不需要在非90度的角度上旋转它。

I have such code in my old app. But this code can be simplified because you have no need to rotate it on non-90 degrees angle.

- (UIImage *)rotateImage:(UIImage *)image onDegrees:(float)degrees
{
    CGFloat rads = M_PI * degrees / 180;
    float newSide = MAX([image size].width, [image size].height);
    CGSize size =  CGSizeMake(newSide, newSide);
    UIGraphicsBeginImageContext(size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(ctx, newSide/2, newSide/2);
    CGContextRotateCTM(ctx, rads);
    CGContextDrawImage(UIGraphicsGetCurrentContext(),CGRectMake(-[image size].width/2,-[image size].height/2,size.width, size.height),image.CGImage);
    UIImage *i = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return i;
}

这篇关于如何将UIImage和图像数据旋转90度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆