ios - Swift - 以度为单位的图像旋转 - 变形结果

标签 ios swift image rotation uigraphicscontext

我发现了几种在 Swift 中按度数旋转图像的方法,但没有一种方法始终有效。我想将图像向左或向右旋转 90 度。我认为最好的是这个:

  class func imageRotated(_ oldImage: UIImage, byDegrees 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(M_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(M_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
  }

它可以正常工作并做我想做的事,但很少有几次它会导致结果变形,因此它不会旋转,而只是更改图像大小,或者旋转 180 度并更改图像大小。

旋转前:

Image before rotation

旋转后(菜单丢失,因为它自动隐藏):

Image after rotation

奇怪的是,当我拍摄人像照片时会发生这种情况。当我拍摄横向照片时,旋转效果很好(即使我尝试旋转几次,即使我从横向旋转到纵向然后再旋转到横向,它仍然可以正常工作)。

我看到一些关于自动布局约束问题的讨论,但我不确定这是否是我的问题以及为什么它在某些情况下有效而在某些情况下无效。我在集合单元格中有照片,每个单元格都有第一个 UIScrollView,而在 UIScrollView 中有带照片的 UIImageView。所有约束都设置为边界。

如果有帮助,我只想旋转 90 或 -90 度。这可能会使方法更简单并且更容易修复。

感谢您的帮助。

最佳答案

实际上,最简单也可能是最好的方法是创建一个 View ,在其上放置一个 ImageView ,旋转 ImageView ,调整父 View 的大小并创建该 View 的屏幕截图。

您实际上并没有在 View 层次结构中添加这些 View ,您只是使用它们来轻松生成图像。

为什么这是一个好的程序是因为 UIView 已经编写了所有代码以完全支持核心图形。所以没有任何缺点,您可以确定这些程序已经由 Apple 完善。

那么试试这个:

    func transformImage(image: UIImage?, transform: CGAffineTransform) -> UIImage? {

        guard let image = image else {
            return nil
        }

        let view = UIView(frame: CGRect.zero)
        let imageView = UIImageView(image: image)
        imageView.transform = transform
        view.addSubview(imageView)
        imageView.frame.origin = CGPoint.zero
        view.frame = CGRect(x: 0.0, y: 0.0, width: imageView.frame.size.width, height: imageView.frame.size.height)


        UIGraphicsBeginImageContext(view.frame.size)
        view.layer.render(in: UIGraphicsGetCurrentContext()!)
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        if let cgImage = newImage?.cgImage {
            return UIImage(cgImage: cgImage)
        } else {
            return nil
        }

    }

至于您程序中的问题,很可能是因为您放弃了 UIImage 的转换。 CGImage 基本上是原始图像表示,它不知道旋转。 UIImage 虽然有 imageOrientation。因此,您需要做的是从图像方向生成变换矩阵并使用它来解决这个问题。

因此,当您使用设备拍摄图像时,CGImage 将始终具有相同的方向(横向左侧或右侧)。但是 UIImage 会改变图像方向,然后用于表示。

关于ios - Swift - 以度为单位的图像旋转 - 变形结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42413388/

相关文章:

swift - 如何让 NSTextField 换行在可变行高表中

SwiftUI : ViewModifier where content is a Shape

iphone - 在 UIView 上显示 XIB!如何设置?

ios - 如何最小化ios应用程序

swift - 将 CGFloat 数组标准化为 UIView 框架高度

android - 更改图像某些部分的颜色

javascript - jQuery 中的函数不返回变量

c++ - BMP Exporter 不工作 C++

ios - 尝试将 NSDate 转换为字符串时获取下一个日期

ios - 使用 Xamarin 的通用约束