Rotate 90 degrees changes image size and cropping - Swift 3

时间:2017-05-16 09:30:32

标签: ios swift3 uiimage uiimagepickercontroller

I use this code for rotate my image and send to azure storage.

My code:

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
}

But this code Shrinks my image and crop. Can someone help me with this problem?

1 个答案:

答案 0 :(得分:1)

我会在这里粘贴一个完整的代码,用于图像的每个可能的旋转。对于那些90度更简单,因为您不需要背景,您可以简单地使用图像初始化图像视图,对其应用变换并返回其快照。

任何轮换的代码:

class ImageTools {

    static func rotatedImage(image: UIImage?, byDegress degress: CGFloat, backroundColor: UIColor? = nil) -> UIImage? {
        guard let image = image else {
            return nil
        }

        let angle = degress * CGFloat.pi / 180.0

        let imageView = UIImageView(image: image)

        let transform = CGAffineTransform(rotationAngle: angle)

        let transformedSize: (CGSize) = {
            let leftFromCenter = CGPoint(x: -imageView.frame.size.width*0.5, y: imageView.frame.size.height*0.5)
            let rightFromCenter = CGPoint(x: imageView.frame.size.width*0.5, y: imageView.frame.size.height*0.5)

            let leftTransformed = leftFromCenter.applying(transform)
            let rightTransformed = rightFromCenter.applying(transform)

            return CGSize(width: max(abs(leftTransformed.x*2.0), abs(rightTransformed.x*2.0)), height: max(abs(leftTransformed.y*2.0), abs(rightTransformed.y*2.0)))
        }()

        let canvasView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: transformedSize.width, height: transformedSize.height))
        canvasView.backgroundColor = backroundColor
        canvasView.addSubview(imageView)
        imageView.center = CGPoint(x: transformedSize.width*0.5, y: transformedSize.height*0.5)
        imageView.transform = transform

        return viewSnapshot(view: canvasView)
    }

    private static func viewSnapshot(view: UIView?) -> UIImage? {
        guard let view = view else {
            return nil
        }

        UIGraphicsBeginImageContext(view.bounds.size)

        guard let context = UIGraphicsGetCurrentContext() else {
            return nil
        }

        view.layer.render(in: context)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }

}

此程序在上下文程序中使用UIView绘制,我认为这是最好的。在背景石英芯中使用甚至更好,该方法被优化并且表现很好。所以没有必要用旋转,平移和刻度来拉你的头发。如果图像已经有方向,也不会出现问题。