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

Posted

技术标签:

【中文标题】旋转 90 度更改图像大小和裁剪 - Swift 3【英文标题】:Rotate 90 degrees changes image size and cropping - Swift 3 【发布时间】:2017-05-16 09:30:31 【问题描述】:

我使用此代码来旋转我的图像并发送到天蓝色存储。

我的代码:

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º。这基本上是因为当我拍摄照片时,它是水平发送的,我希望它是垂直的 从 UIImage 获取 cgImage 并使用***.com/questions/10544887/rotating-a-cgimage 【参考方案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 在上下文中绘制过程,我认为这是目前最好的。在背景中使用甚至更好的石英芯,该方法得到了优化并且性能非常好。因此,无需旋转、平移和缩放头发。如果图像已经有方向,也没有问题。

【讨论】:

以上是关于旋转 90 度更改图像大小和裁剪 - Swift 3的主要内容,如果未能解决你的问题,请参考以下文章

如何将计算机绘图旋转 90 度

Leecode-48:旋转图像(矩阵顺时针旋转90度)

通过transpose和flip实现图像旋转90/180/270度

CGAffineTransform 旋转 90 度

OpenCV图像旋转90度函数cv2.transpose导致镜像翻转

图片旋转90度解决的方法