【发布时间】:2017-05-16 09:30:31
【问题描述】:
我使用此代码来旋转我的图像并发送到天蓝色存储。
我的代码:
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.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.pi / 180))
//Now, draw the rotated/scaled image into the context
bitmap.scaleBy(x: 1.0, y: -1.0)
bitmap.draw(oldImage.cgImage!, in: CGRect(x: -oldImage.size.width / 2, y: -oldImage.size.height / 2, width: oldImage.size.width, height: oldImage.size.height))
let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return newImage
}
但是这段代码会缩小我的图像并进行裁剪。 有人可以帮我解决这个问题吗?
【问题讨论】:
-
角度可以是任何角度,还是只寻找 90 度的角度(90、180、270、-90、-180、-270、0)?
-
@MaticOblak,只有 90º。这基本上是因为当我拍摄照片时,它是水平发送的,我希望它是垂直的
-
从 UIImage 获取 cgImage 并使用stackoverflow.com/questions/10544887/rotating-a-cgimage
标签: ios swift3 uiimage uiimagepickercontroller