在Swift中旋转UIImage [英] Rotating UIImage in Swift

查看:122
本文介绍了在Swift中旋转UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Xcode 6.0.1和Swift。我有一个UIImage,我想用旧图像作为源制作另一个图像,新图像以某种方式旋转......比如垂直翻转。



<这个问题已经在几个月前回答了。但是,即使情况相同,该解决方案对我也不起作用。



当我有

  var image = UIImage(CGImage:otherImage.CGImage,scale:1.0,orientation:.DownMirrored)

Xcode抱怨在调用中有一个额外参数'缩放'。在使用Apple文档进行检查后,这没有任何意义,因为该版本的初始化程序确实采用了这三个参数。省略比例和方向参数确实解决了问题,但阻止我进行轮换。



我能找到的唯一其他参考是这个人,他们有同样的问题。



您怎么看?



我确实需要在这个版本的Xcode上运行,所以如果有另一种方法来执行旋转(我还没有找到一个但是这将是有用的。

解决方案

这是UIImage的一个简单扩展:

  // ImageRotation.swift 

import UIKit

扩展名UIImage {
public func imageRotatedByDegrees(度: CGFloat,翻转:Bool) - > UIImage {
let radiansToDegrees:(CGFloat) - > CGFloat = {
返回$ 0 *(180.0 / CGFloat(M_PI))
}
let degreesToRadians:(CGFloat) - > CGFloat = {
返回$ 0 / 180.0 * CGFloat(M_PI)
}

//为我们的绘图空间计算旋转视图的包含框的大小
let rotatingViewBox = UIView(frame:CGRect(origin:CGPointZero,size:size))
let t = CGAffineTransformMakeRotation(degreesToRadians(degrees));
rotateViewBox.transform = t
let rotateSize = rotatingViewBox.frame.size

//创建位图上下文
UIGraphicsBeginImageContext(rotatingSize)
let bitmap = UIGraphicsGetCurrentContext()

//将原点移动到图像的中间位置,这样我们就会围绕中心旋转和缩放。
CGContextTranslateCTM(bitmap,rotatingSize.width / 2.0,rotatingSize.height / 2.0);

// //旋转图像上下文
CGContextRotateCTM(bitmap,degreesToRadians(degrees));

//现在,将旋转/缩放的图像绘制到上下文
var yFlip:CGFloat

if(flip){
yFlip = CGFloat( -1.0)
} else {
yFlip = CGFloat(1.0)
}

CGContextScaleCTM(位图,yFlip,-1.0)
CGContextDrawImage(位图, CGRectMake(-size.width / 2,-size.height / 2,size.width,size.height),CGImage)

let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

返回newImage
}
}

来源



使用它:

  rotatingPhoto = rotatingPhoto?.imageRotatedByDegrees(90,flip:false)

如果将flip设置为true,前者将旋转图像并翻转它。


I'm using Xcode 6.0.1 with Swift. I have a UIImage, and I would like to make another image using the old image as a source, with the new image being rotated in some way... say flipped vertically.

This question was already answered a few months ago. However, that solution doesn't work for me, even though the situation is identical.

When I have

var image = UIImage(CGImage: otherImage.CGImage, scale: 1.0, orientation: .DownMirrored)

Xcode complains that there's an "Extra argument 'scale' in call". After checking with the Apple documentation, this makes no sense, as that version of the initializer does take those three arguments. Leaving out the scale and orientation arguments does fix the problem, but prevents me from doing the rotation.

The only other reference to this that I can find is this guy, who had the same problem.

What do you think?

I do need this to run on this version of Xcode, so if there's an alternate way to perform the rotation (I haven't found one yet) that would be useful.

解决方案

Here is a simple extension to UIImage:

//ImageRotation.swift

import UIKit

extension UIImage {  
    public func imageRotatedByDegrees(degrees: CGFloat, flip: Bool) -> UIImage {
        let radiansToDegrees: (CGFloat) -> CGFloat = {
            return $0 * (180.0 / CGFloat(M_PI))
        }
        let degreesToRadians: (CGFloat) -> CGFloat = {
            return $0 / 180.0 * CGFloat(M_PI)
        }

        // calculate the size of the rotated view's containing box for our drawing space
        let rotatedViewBox = UIView(frame: CGRect(origin: CGPointZero, size: size))
        let t = CGAffineTransformMakeRotation(degreesToRadians(degrees));
        rotatedViewBox.transform = t
        let rotatedSize = rotatedViewBox.frame.size

        // Create the bitmap context
        UIGraphicsBeginImageContext(rotatedSize)
        let 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.0, rotatedSize.height / 2.0);

        //   // Rotate the image context
        CGContextRotateCTM(bitmap, degreesToRadians(degrees));

        // Now, draw the rotated/scaled image into the context
        var yFlip: CGFloat

        if(flip){
            yFlip = CGFloat(-1.0)
        } else {
            yFlip = CGFloat(1.0)
        }

        CGContextScaleCTM(bitmap, yFlip, -1.0)
        CGContextDrawImage(bitmap, CGRectMake(-size.width / 2, -size.height / 2, size.width, size.height), CGImage)

        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return newImage
    }
}

(Source)

Use it with:

rotatedPhoto = rotatedPhoto?.imageRotatedByDegrees(90, flip: false) 

The former will rotate an image and flip it if flip is set to true.

这篇关于在Swift中旋转UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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