이 코드를 사용하여 이미지를 회전하고 하늘색 저장 장치에 보냅니다.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.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º. 이것은 기본적으로 사진을 찍을 때 가로로 보내고 세로로 보냅니다. – HideCode
UIImage에서 cgImage를 가져 와서 http://stackoverflow.com/questions/10544887/rotating-a-cgimage – Spads