我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))
http:// stackoverflow。com/a/33479054/291240 –
當出現錯誤時,請不要忘記告訴人們他們是誰! –
從相機捕捉後管理圖像的條件。並嘗試此鏈接:http://stackoverflow.com/questions/9324130/iphone-image-captured-from-camera-rotate-90-degree-automatically它將有助於理清問題。 –