【问题标题】:Rotate image with UIGraphicsImageRenderer?使用 UIGraphicsImageRenderer 旋转图像?
【发布时间】:2016-12-29 13:44:23
【问题描述】:

UIGraphicsImageRenderer 是 iOS 10 中新引入的。我想知道是否有可能用它旋转UIImage(任何自定义角度)。我知道有classic way 和CGContextRotateCTM。

【问题讨论】:

    标签: swift uikit ios10 image-rendering


    【解决方案1】:

    基于@reza23 的回答。你不需要调用 UIGraphicsGetCurrentContext,你可以使用渲染器的上下文。

    extension UIImage
    {
        public func rotate(angle:CGFloat)->UIImage
        {
            let radians = CGFloat(angle * .pi) / 180.0 as CGFloat
            
            var newSize = CGRect(origin: CGPoint.zero, size: self.size).applying(CGAffineTransform(rotationAngle: radians)).size
            
            // Trim off the extremely small float value to prevent core graphics from rounding it up
            newSize.width = floor(newSize.width)
            newSize.height = floor(newSize.height)
            let renderer = UIGraphicsImageRenderer(size:newSize)
            
            let image = renderer.image
            { rendederContext in
                
                let context = rendederContext.cgContext
                //rotate from center
                context.translateBy(x: newSize.width/2, y: newSize.height/2)
                context.rotate(by: radians)
                draw(in:  CGRect(origin: CGPoint(x: -self.size.width/2, y: -self.size.height/2), size: size))
            }
            
            return image
        }
    }
    

    【讨论】:

      【解决方案2】:

      您可以设置 UIGraphicsImageRenderer 来创建图像,然后调用 UIGraphicsGetCurrentContext() 并旋转上下文

      let renderer = UIGraphicsImageRenderer(size:sizeOfImage)
      let image = renderer.image(actions: { _ in
          let context = UIGraphicsGetCurrentContext()
      
          context?.translateBy(x: orgin.x, y: orgin.y)
          context?.rotate(by: angle)
          context?.draw(image.cgImage!, in: CGRect(origin: CGPoint(x: -orgin.x,y: -orgin.y), size: size))
      }
      return image
      

      【讨论】:

      • 我认为您应该使用渲染器的 context.cgContext 属性,而不是获取当前上下文,因为它是自动管理的。
      • 自 2021.10.23 起,此代码不再有效。
      【解决方案3】:

      浏览文档,并且由于缺乏对这个问题的答复,我认为新的UIGraphicsImageRenderer 是不可能的。这是我在一天结束时解决它的方法:

      func changeImageRotation(forImage image:UIImage, rotation alpha:CGFloat) -> UIImage{
      
          var newSize:CGSize{
              let a = image.size.width
              let b = image.size.height
      
              let width = abs(cos(alpha)) * a + abs(sin(alpha)) * b
              let height = abs(cos(alpha)) * b + abs(sin(alpha)) * a
      
              return CGSize(width: width, height: height)
          }
      
          let size = newSize
          let orgin = CGPoint(x: size.width/2, y: size.height/2)
      
          UIGraphicsBeginImageContext(size)
          let context = UIGraphicsGetCurrentContext()
      
          context?.translateBy(x: orgin.x, y: orgin.y)
          context?.rotate(by: alpha)
          context?.draw(image.cgImage!, in: CGRect(origin: CGPoint(x: -orgin.x,y: -orgin.y), size: size))
      
          let newImage = UIGraphicsGetImageFromCurrentImageContext();
          UIGraphicsEndImageContext();
      
          return newImage!
      }
      

      New Size对应于绘制旋转图像而不改变其整体大小所需的矩形区域。然后在中心旋转和绘制图像。有关这方面的更多信息,请参阅此post。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-12-15
        • 2016-04-08
        • 2011-04-26
        • 2010-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多