在Swift中旋转UIImage

std*_*nsw 45 uiimage ios swift

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

几个月前这个问题已得到解答.但是,即使情况相同,该解决方案也不适用于我.

当我有

var image = UIImage(CGImage: otherImage.CGImage, scale: 1.0, orientation: .DownMirrored)
Run Code Online (Sandbox Code Playgroud)

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

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

你怎么看?

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

Jos*_*eld 58

Swift 4解决方案

这是最简单的解决方案

extension UIImage {
    func rotate(radians: Float) -> UIImage? {
        var newSize = CGRect(origin: CGPoint.zero, size: self.size).applying(CGAffineTransform(rotationAngle: CGFloat(radians))).size
        // Trim off the extremely small float value to prevent core graphics from rounding it up
        newSize.width = floor(newSize.width)
        newSize.height = floor(newSize.height)

        UIGraphicsBeginImageContextWithOptions(newSize, false, self.scale)
        let context = UIGraphicsGetCurrentContext()!

        // Move origin to middle
        context.translateBy(x: newSize.width/2, y: newSize.height/2)
        // Rotate around middle
        context.rotate(by: CGFloat(radians))
        // Draw the image at its center
        self.draw(in: CGRect(x: -self.size.width/2, y: -self.size.height/2, width: self.size.width, height: self.size.height))

        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return newImage
    }
}
Run Code Online (Sandbox Code Playgroud)

要使用此解决方案,您可以执行以下操作

let image = UIImage(named: "image.png")!
let newImage = image.rotate(radians: .pi/2) // Rotate 90 degrees
Run Code Online (Sandbox Code Playgroud)

  • `.pi` 是 180 度,`.pi/2` 是 90 度,`.pi/4` 是 45 度,...,`.pi/180` 是 1 度。 (5认同)
  • 这是一个很好的解决方案。我使用了另一个在旋转后切割我的图像,但这个效果很好。 (4认同)
  • 处理不同方向的 UIImage 时要小心。self.draw() 将自动以 .up 方向绘制图像。 (2认同)
  • 我正在使用此尝试在自定义annotationView中旋转uiimage.图像是一辆摩托车的.png.它没有旋转图像......它只是返回一个黑色方块? (2认同)
  • 如果您使用 SwiftUI,请将 SwiftUI 导入您的 UIImage 扩展而不是 UIKit,并将函数签名更改为:“rotate(angle: Angle)”。在函数内部,执行:“let radians = angle.radians”。现在您可以使用以下命令调用该函数:“image.rotate(angle: . Degrees(90))”或“image.rotate(angle: . Degrees(-90))” (2认同)

con*_*ile 46

这是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
    }
}
Run Code Online (Sandbox Code Playgroud)

(来源)

使用它:

rotatedPhoto = rotatedPhoto?.imageRotatedByDegrees(90, flip: false) 
Run Code Online (Sandbox Code Playgroud)

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

  • Xcode给出了转换Swift 3代码的建议,除了这行,`CGContextDrawImage(bitmap,CGRectMake(-size.width/2,-size.height/2,size.width,size.height),CGImage)`.将其更改为`bitmap?.draw(cgImage!,in:CGRect(x:-size.width/2,y:-size.height/2,width:size.width,height:size.height)) (7认同)
  • 容易,翻转工作真棒.旋转的问题是旋转图像,但是 - 它改变了宽高比并"破坏它",你能检查它并修改代码吗?这会很棒.谢谢! (4认同)
  • `UIGraphicsBeginImageContext(rotatingSize)`应该是`UIGraphicsBeginImageContextWithOptions(rotatingSize,false,self.scale)`来保持规模 (2认同)

Ste*_*rch 30

extension UIImage {
    struct RotationOptions: OptionSet {
        let rawValue: Int

        static let flipOnVerticalAxis = RotationOptions(rawValue: 1)
        static let flipOnHorizontalAxis = RotationOptions(rawValue: 2)
    }

    func rotated(by rotationAngle: Measurement<UnitAngle>, options: RotationOptions = []) -> UIImage? {
        guard let cgImage = self.cgImage else { return nil }

        let rotationInRadians = CGFloat(rotationAngle.converted(to: .radians).value)
        let transform = CGAffineTransform(rotationAngle: rotationInRadians)
        var rect = CGRect(origin: .zero, size: self.size).applying(transform)
        rect.origin = .zero

        let renderer = UIGraphicsImageRenderer(size: rect.size)
        return renderer.image { renderContext in
            renderContext.cgContext.translateBy(x: rect.midX, y: rect.midY)
            renderContext.cgContext.rotate(by: rotationInRadians)

            let x = options.contains(.flipOnVerticalAxis) ? -1.0 : 1.0
            let y = options.contains(.flipOnHorizontalAxis) ? 1.0 : -1.0
            renderContext.cgContext.scaleBy(x: CGFloat(x), y: CGFloat(y))

            let drawRect = CGRect(origin: CGPoint(x: -self.size.width/2, y: -self.size.height/2), size: self.size)
            renderContext.cgContext.draw(cgImage, in: drawRect)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

你可以像这样使用它:

let rotatedImage = UIImage(named: "my_amazing_image")?.rotated(by: Measurement(value: 48.0, unit: .degrees))
Run Code Online (Sandbox Code Playgroud)

指定翻转标志:

let flippedImage = UIImage(named: "my_amazing_image")?.rotated(by: Measurement(value: 48.0, unit: .degrees), options: [.flipOnVerticalAxis])
Run Code Online (Sandbox Code Playgroud)


小智 9

当其中一个输入类型不正确时,通常会发生'调用中的额外参数'.

您可以使用修复代码

var image = UIImage(CGImage: otherImage.CGImage, scale: CGFloat(1.0), orientation: .DownMirrored)
Run Code Online (Sandbox Code Playgroud)


Pet*_*inz 9

斯威夫特3:

向右旋转:

let image = UIImage(cgImage: otherImage.cgImage!, scale: CGFloat(1.0), orientation: .right)
Run Code Online (Sandbox Code Playgroud)

在游乐场测试:

// MyPlayground.playground

import UIKit
import PlaygroundSupport

let view = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 500))
view.backgroundColor = UIColor.white
PlaygroundPage.current.liveView = view

let otherImage = UIImage(named: "burger.png", in: Bundle.main,
                         compatibleWith: nil)
let imageViewLeft = UIImageView(image: UIImage(cgImage: (otherImage?.cgImage!)!, scale: CGFloat(1.0), orientation: .left)) 
let imageViewRight = UIImageView(image: UIImage(cgImage: (otherImage?.cgImage!)!, scale: CGFloat(1.0), orientation: .right))

view.addSubview(imageViewLeft)
view.addSubview(imageViewRight)
view.layoutIfNeeded()
Run Code Online (Sandbox Code Playgroud)


Osc*_*car 6

@confile 答案更新到 Swift 4

import UIKit

extension UIImage {

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

        // calculate the size of the rotated view's containing box for our drawing space
        let rotatedViewBox = UIView(frame: CGRect(origin: .zero, size: size))
        let t = CGAffineTransform(rotationAngle: 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.
        bitmap?.translateBy(x: rotatedSize.width / 2.0, y: rotatedSize.height / 2.0)

        //   // Rotate the image context
        bitmap?.rotate(by: 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)
        }

        bitmap?.scaleBy(x: yFlip, y: -1.0)
        let rect = CGRect(x: -size.width / 2, y: -size.height / 2, width: size.width, height: size.height)

        bitmap?.draw(cgImage!, in: rect)

        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return newImage!
    }
}
Run Code Online (Sandbox Code Playgroud)


Khu*_*ong 5

试试下面的代码,它对我有用:

@IBAction func btnRotateImagePressed(sender: AnyObject) {
    if let originalImage = self.imageView.image {

        let rotateSize = CGSize(width: originalImage.size.height, height: originalImage.size.width)
        UIGraphicsBeginImageContextWithOptions(rotateSize, true, 2.0)
        if let context = UIGraphicsGetCurrentContext() {
            CGContextRotateCTM(context, 90.0 * CGFloat(M_PI) / 180.0)
            CGContextTranslateCTM(context, 0, -originalImage.size.height)
            originalImage.drawInRect(CGRectMake(0, 0, originalImage.size.width, originalImage.size.height))
            self.imageView.image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Ped*_*rim 5

不要使用,UIGraphicsBeginImageContext因为它会破坏您的载体的质量,请使用UIGraphicsBeginImageContextWithOptions. 这是我在 Swift 3/4 中的实现:

extension UIImage {

  func rotated(degrees: CGFloat) -> UIImage? {

    let degreesToRadians: (CGFloat) -> CGFloat = { (degrees: CGFloat) in
      return degrees / 180.0 * CGFloat.pi
    }

    // Calculate the size of the rotated view's containing box for our drawing space
    let rotatedViewBox: UIView = UIView(frame: CGRect(origin: .zero, size: size))
    rotatedViewBox.transform = CGAffineTransform(rotationAngle: degreesToRadians(degrees))
    let rotatedSize: CGSize = rotatedViewBox.frame.size

    // Create the bitmap context
    UIGraphicsBeginImageContextWithOptions(rotatedSize, false, 0.0)

    guard let bitmap: CGContext = UIGraphicsGetCurrentContext(), let unwrappedCgImage: CGImage = cgImage else {
      return nil
    }

    // Move the origin to the middle of the image so we will rotate and scale around the center.
    bitmap.translateBy(x: rotatedSize.width/2.0, y: rotatedSize.height/2.0)

    // Rotate the image context
    bitmap.rotate(by: degreesToRadians(degrees))

    bitmap.scaleBy(x: CGFloat(1.0), y: -1.0)

    let rect: CGRect = CGRect(
        x: -size.width/2,
        y: -size.height/2,
        width: size.width,
        height: size.height)

    bitmap.draw(unwrappedCgImage, in: rect)

    guard let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext() else {
      return nil
    }

    UIGraphicsEndImageContext()

    return newImage
  }
Run Code Online (Sandbox Code Playgroud)