How to rotate an image 90 degrees on iOS?

后端 未结 14 1911
野的像风
野的像风 2020-11-29 00:55

What I want to do is take a snapshot from my camera , send it to a server and then the server sends me back the image on a viewController. If the image is in portrait mode t

14条回答
  •  悲&欢浪女
    2020-11-29 01:33

    Doing it in the Image Level, in Swift 5:

    extension UIImage{
        
        
        func imageRotated(by radian: CGFloat) -> UIImage{
            
            let rotatedSize = CGRect(origin: .zero, size: size)
                .applying(CGAffineTransform(rotationAngle: radian))
                .integral.size
            UIGraphicsBeginImageContext(rotatedSize)
            if let context = UIGraphicsGetCurrentContext() {
                let origin = CGPoint(x: rotatedSize.width / 2.0,
                                     y: rotatedSize.height / 2.0)
                context.translateBy(x: origin.x, y: origin.y)
                context.rotate(by: radian)
                draw(in: CGRect(x: -origin.y, y: -origin.x,
                                width: size.width, height: size.height))
                let rotatedImage = UIGraphicsGetImageFromCurrentImageContext()
                UIGraphicsEndImageContext()
    
                return rotatedImage ?? self
            }
    
            return self
    
        }
    }
    

提交回复
热议问题