如何在Swift 3中旋转图像?

我很快地将图像旋转90度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)) 

设置ImageView图像

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

您可以使用下面的代码Swift 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 } 

你的代码与function并不遥远。 您可以将变换直接应用到位图,而不需要中间视图:

 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 } 

另外,如果你要创build一个旋转图像的函数,通常包括一个clockwise: Bool参数,这个参数会将degrees参数解释为顺时针旋转或不旋转。 实施和适当的转换为弧度我留给你。

另外请注意,假设oldImage.size非零,这有点手动波动。 如果是,强制解包UIGraphicsGetCurrentContext()! 可能会崩溃。 你应该validationoldImage的大小,如果它是无效的只是返回oldImage

你的问题是你使用CGRect的不存在的初始值设定项。 如果你想使用CGRect(origin:size 🙂 ,那么创build原点,没有宽度和高度参数。 或者删除大小参数并使用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) 

您可以先在imageView中设置图像,然后在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!