旋转 90 度更改图像大小和裁剪 - Swift 3

Rotate 90 degrees changes image size and cropping - Swift 3

我使用此代码旋转我的图像并发送到 Azure 存储。

我的代码:

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 度更简单一些,因为您不需要背景,您可以简单地用图像初始化图像视图,对其应用变换和 return 它的快照。

任意旋转的代码:

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 在上下文程序中绘制,我认为这是目前最好的。在使用甚至更好的石英芯的背景下,该方法被优化并且性能显着。因此,无需为旋转、平移和缩放而费神。如果图像已经有方向也没有问题。