2016-11-30 126 views
19

我unbale将图像旋转到迅速3.I 90度已经写了下面的代码,但它是有误差&不能编译如何在Swift 3中旋转图像?

func imageRotatedByDegrees(oldImage: UIImage, deg degrees: CGFloat) -> UIImage { 
    //Calculate the size of the rotated view's containing box for our drawing space 
    let rotatedViewBox: UIView = UIView(frame: CGRect(x: 0, y: 0, width: oldImage.size.width, height: oldImage.size.height)) 

    let t: CGAffineTransform = CGAffineTransform(rotationAngle: degrees * CGFloat(M_PI/180)) 
    rotatedViewBox.transform = t 
    let rotatedSize: CGSize = rotatedViewBox.frame.size 
    //Create the bitmap context 
    UIGraphicsBeginImageContext(rotatedSize) 
    let bitmap: CGContext = 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, y: rotatedSize.height/2) 
    //Rotate the image context 
    bitmap.rotate(by: (degrees * CGFloat(M_PI/180))) 
    //Now, draw the rotated/scaled image into the context 
    bitmap.scaleBy(x: 1.0, y: -1.0) 

    bitmap.draw(oldImage, in: CGRect(origin: (x: -oldImage.size.width/2, y: -oldImage.size.height/2, width: oldImage.size.width, height: oldImage.size.height), size: oldImage.cgImage)) 



    let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()! 
    UIGraphicsEndImageContext() 
    return newImage 
    } 
下面的代码

我不能够写&不知道什么是它

bitmap.draw(oldImage, in: CGRect(origin: (x: -oldImage.size.width/2, y: -oldImage.size.height/2, width: oldImage.size.width, height: oldImage.size.height), size: oldImage.cgImage)) 
+0

http:// stackoverflow。com/a/33479054/291240 –

+2

当出现错误时,请不要忘记告诉人们他们是谁! –

+0

从相机捕捉后管理图像的条件。并尝试此链接:http://stackoverflow.com/questions/9324130/iphone-image-captured-from-camera-rotate-90-degree-automatically它将有助于理清问题。 –

回答

8
let angle = CGFloat(M_PI_2) 
    let tr = CGAffineTransform.identity.rotated(by: angle) 
    ImageView.transform = tr 
+5

我想旋转UIImage不在imageview – Techiee

25

集的ImageView图像

ImageView.transform = ImageView.transform.rotated(by: CGFloat(M_PI_2)) 
问题
+2

为什么M_PI_4? 90度= M_PI_2 – alexburtnik

+1

好的......谢谢@alexburtnik –

+3

@JayeshMiruliya,也可以在Swift 3中使用'CGFloat.pi'常量(除以2 ofc) – user28434

0

你的代码与功能并不遥远。您可以将转化为你做直接对位,你并不需要一个中间的看法:

func imageRotatedByDegrees(oldImage: UIImage, deg degrees: CGFloat) -> UIImage { 
    let size = oldImage.size 

    UIGraphicsBeginImageContext(size) 

    let bitmap: CGContext = UIGraphicsGetCurrentContext()! 
    //Move the origin to the middle of the image so we will rotate and scale around the center. 
    bitmap.translateBy(x: size.width/2, y: size.height/2) 
    //Rotate the image context 
    bitmap.rotate(by: (degrees * CGFloat(M_PI/180))) 
    //Now, draw the rotated/scaled image into the context 
    bitmap.scaleBy(x: 1.0, y: -1.0) 

    let origin = CGPoint(x: -size.width/2, y: -size.width/2) 

    bitmap.draw(oldImage.cgImage!, in: CGRect(origin: origin, size: size)) 

    let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()! 
    UIGraphicsEndImageContext() 
    return newImage 
} 

另外,如果你要创建一个旋转图像的功能,这是典型的好形式以包括clockwise: Bool参数,该参数将解释degrees参数为顺时针旋转或不旋转。实施和适当的转换为弧度我留给你。

另请注意,假设oldImage.size不为零,我有点手动波动。如果是这样,解压UIGraphicsGetCurrentContext()!可能会崩溃。您应验证oldImage的尺寸,如果无效,则返回oldImage

+0

虽然功能,此代码不会旋转中心的图像,除非在特殊情况下图像是方形的。我使用了中间方法,它产生了大约中心旋转。 –

0

您的问题是您使用CGRect的不存在的初始值设定项。如果你想使用CGRect(来源:大小:),然后创建原点,没有宽度和高度参数。或者移除尺寸参数并使用CGRect(x:y:width:height :)

只是这一个替换此行

bitmap.draw(oldImage, in: CGRect(origin: (x: -oldImage.size.width/2, y: -oldImage.size.height/2, width: oldImage.size.width, height: oldImage.size.height), size: oldImage.cgImage)) 

let rect = CGRect(origin: CGPoint(x: -oldImage.size.width/2, y: -oldImage.size.height/2), size: oldImage.size) 
bitmap.draw(oldImage.cgImage!, in: rect) 
6

您可以使用下面的代码斯威夫特3:

extension UIImage { 

func fixImageOrientation() -> UIImage? { 
    var flip:Bool = false //used to see if the image is mirrored 
    var isRotatedBy90:Bool = false // used to check whether aspect ratio is to be changed or not 

    var transform = CGAffineTransform.identity 

    //check current orientation of original image 
    switch self.imageOrientation { 
    case .down, .downMirrored: 
     transform = transform.rotated(by: CGFloat(M_PI)); 

    case .left, .leftMirrored: 
     transform = transform.rotated(by: CGFloat(M_PI_2)); 
     isRotatedBy90 = true 
    case .right, .rightMirrored: 
     transform = transform.rotated(by: CGFloat(-M_PI_2)); 
     isRotatedBy90 = true 
    case .up, .upMirrored: 
     break 
    } 

    switch self.imageOrientation { 

    case .upMirrored, .downMirrored: 
     transform = transform.translatedBy(x: self.size.width, y: 0) 
     flip = true 

    case .leftMirrored, .rightMirrored: 
     transform = transform.translatedBy(x: self.size.height, y: 0) 
     flip = true 
    default: 
     break; 
    } 

    // calculate the size of the rotated view's containing box for our drawing space 
    let rotatedViewBox = UIView(frame: CGRect(origin: CGPoint(x:0, y:0), size: size)) 
    rotatedViewBox.transform = transform 
    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); 

    // 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) 

    //check if we have to fix the aspect ratio 
    if isRotatedBy90 { 
     bitmap?.draw(self.cgImage!, in: CGRect(x: -size.width/2, y: -size.height/2, width: size.height,height: size.width)) 
    } else { 
     bitmap?.draw(self.cgImage!, in: CGRect(x: -size.width/2, y: -size.height/2, width: size.width,height: size.height)) 
    } 

    let fixedImage = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 

    return fixedImage 
} 
+1

完美,谢谢 –

2

您可以先设置你的图像中imageView和旋转让你的图像在图像查看

这里:

let customImageView = UIImageView() 
customImageView.frame = CGRect.init(x: 0, y: 0, w: 250, h: 250) 
customImageView.image = UIImage(named: "yourImage") 

customImageView .transform = customImageView .transform.rotated(by: CGFloat.init(M_PI_2)) 
var rotatedImage = UIImage() 
rotatedImage = customImageView.image! 
0

这是UIImage的扩展,针对雨燕4.0,可以无需一个UIImageView只是旋转图像。测试成功,图像旋转,而不仅仅是其exif数据更改。

import UIKit 

extension UIImage { 
    func rotate(radians: CGFloat) -> UIImage { 
     let rotatedSize = CGRect(origin: .zero, size: size).applying(CGAffineTransform(rotationAngle: CGFloat(radians))).integral.size 
     UIGraphicsBeginImageContext(rotatedSize) 
     if let context = UIGraphicsGetCurrentContext() { 
      let origin = CGPoint(x: rotatedSize.width/2.0, y: rotatedSize.height/2.0) 
      context.translateBy(x: origin.x, y: origin.y) 
      context.rotate(by: radians) 
      draw(in: CGRect(x: -origin.x, y: -origin.y, width: size.width, height: size.height)) 
      let rotatedImage = UIGraphicsGetImageFromCurrentImageContext() 
      UIGraphicsEndImageContext() 

      return rotatedImage ?? self 
     } 

     return self 
    } 
} 

要进行180度旋转,你可以这样调用:

let rotatedImage = image.rotate(radians: .pi) 

如果出于某种原因,它不能旋转,原始图像将被退回。