以下扩展程序文件会颠倒生成我的图像。我需要做的就是将图像翻转180度。
case .landscapeLeft:
var transform: CGAffineTransform = CGAffineTransform.identity
transform = transform.translatedBy(x: self.size.width, y: self.size.height)
transform = transform.rotated(by: CGFloat(Double.pi/2))
guard let cgImage = self.cgImage, let colorSpace = cgImage.colorSpace, let context: CGContext = CGContext(data: nil, width: Int(self.size.width), height: Int(self.size.height), bitsPerComponent: cgImage.bitsPerComponent, bytesPerRow: 0, space: colorSpace, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) else { return self }
context.concatenate(transform)
context.draw(cgImage, in: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height))
guard let transformed = context.makeImage() else { return self }
return UIImage(cgImage: transformed)
return imageResult!
我曾尝试使用3 * Double.pi / 2,但图像视图中没有图像。对于我的代码,在图像视图上获取图像的唯一数学公式是double.pi / 2。
答案 0 :(得分:0)
您的代码会围绕图像的原点旋转图像,该图像恰好是左上角。您需要在应用旋转之前将原点转换为图像的中心,然后将原点转换回左上角。如果用以下代码替换变换结构,它应该可以工作
var transform: CGAffineTransform = CGAffineTransform.identity
transform = transform.translatedBy(x: self.size.width/2, y: self.size.height/2)
transform = transform.rotated(by: angle)
transform = transform.translatedBy(x: -self.size.width/2, y: -self.size.height/2)