Ios 如何旋转图像?

Ios 如何旋转图像?,ios,objective-c,uiimage,Ios,Objective C,Uiimage,我想从另一个图像创建一个新的UIImage,该图像旋转到45°(在其左下角,顺时针方向)。旧图像周围的空间将填充为白色左右。在我上传的图像中,旧图像是蓝色的,新图像是我链接的实际图像,包括白色部分 如果您只想更改图像的显示方式,请变换显示图像的图像视图 如果确实需要新的旋转图像,请在转换后的图形上下文中重新绘制图像。如果只想旋转用于显示图像的UIImageView,可以执行以下操作: #define DegreesToRadians(x) ((x) * M_PI / 180.0) //put t

我想从另一个图像创建一个新的
UIImage
,该图像旋转到45°(在其左下角,顺时针方向)。旧图像周围的空间将填充为白色左右。在我上传的图像中,旧图像是蓝色的,新图像是我链接的实际图像,包括白色部分


如果您只想更改图像的显示方式,请变换显示图像的图像视图


如果确实需要新的旋转图像,请在转换后的图形上下文中重新绘制图像。

如果只想旋转用于显示图像的
UIImageView
,可以执行以下操作:

#define DegreesToRadians(x) ((x) * M_PI / 180.0) //put this at the top of your file
imageView.transform = CGAffineTransformMakeRotation(DegreesToRadians(45));
但如果要旋转实际图像,请执行以下操作:

- (UIImage *)image:(UIImage *)image rotatedByDegrees:(CGFloat)degrees
{
    // calculate the size of the rotated view's containing box for our drawing space
    UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
    CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees));
    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, DegreesToRadians(degrees));

    // Now, draw the rotated/scaled image into the context
    CGContextScaleCTM(bitmap, 1.0, -1.0);
    CGContextDrawImage(bitmap, CGRectMake(-image.size.width / 2, -image.size.height / 2, image.size.width, image.size.height), [image CGImage]);

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

以上代码改编自狮子的回答

与斯威夫特在操场上玩了一会儿,以下是我的解决方案:

func rotateImage(image: UIImage!, var rotationDegree: CGFloat) -> UIImage {
    // 180 degress = 540 degrees, that's why we calculate modulo
    rotationDegree = rotationDegree % 360

    // If degree is negative, then calculate positive
    if rotationDegree < 0.0 {
        rotationDegree = 360 + rotationDegree
    }

    // Get image size
    let size = image.size
    let width = size.width
    let height = size.height

    // Get degree which we will use for calculation
    var calcDegree = rotationDegree
    if calcDegree > 90 {
        calcDegree = 90 - calcDegree % 90
    }

    // Calculate new size
    let newWidth = width * CGFloat(cosf(Float(calcDegree.degreesToRadians))) + height * CGFloat(sinf(Float(calcDegree.degreesToRadians)))
    let newHeight = width * CGFloat(sinf(Float(calcDegree.degreesToRadians))) + height * CGFloat(cosf(Float(calcDegree.degreesToRadians)))
    let newSize = CGSize(width: newWidth, height: newHeight)

    // Create context using new size, make it opaque, use screen scale
    UIGraphicsBeginImageContextWithOptions(newSize, true, UIScreen.mainScreen().scale)

    // Get context variable
    let context = UIGraphicsGetCurrentContext()

    // Set fill color to white (or any other)
    // If no color needed, then set opaque to false when initialize context
    CGContextSetFillColorWithColor(context, UIColor.whiteColor().CGColor)
    CGContextFillRect(context, CGRect(origin: CGPointZero, size: newSize))

    // Rotate context and draw image
    CGContextTranslateCTM(context, newSize.width * 0.5, newSize.height * 0.5)
    CGContextRotateCTM(context, rotationDegree.degreesToRadians);
    CGContextTranslateCTM(context, newSize.width * -0.5, newSize.height * -0.5)
    image.drawAtPoint(CGPoint(x: (newSize.width - size.width) / 2.0, y: (newSize.height - size.height) / 2.0))

    // Get image from context
    let returnImage = UIGraphicsGetImageFromCurrentImageContext()

    // End graphics context
    UIGraphicsEndImageContext()

    return returnImage
}

为了更好地理解图像旋转后我是如何计算新闻大小的,我建议使用这个属性。

使用
transform
属性。示例代码和我书中的进一步解释:
extension CGFloat {
    var degreesToRadians : CGFloat {
        return self * CGFloat(M_PI) / 180.0
    }
}