2017-05-16 94 views
0

我使用此代码旋转我的图像并发送到天蓝色的存储。旋转90度改变图像大小和裁剪 - Swift 3

我的代码:

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 
} 

但这个代码缩小我的形象和作物。 有人可以帮我解决这个问题吗?

+0

角度可以是任何角度,或者你只是在寻找90度角度(90,180,270,-90,-180,-270,0)? –

+0

@MaticOblak,只有90º。这基本上是因为当我拍摄照片时,它被水平发送,我想垂直 – HideCode

+0

从UIImage获取cgImage并使用http://stackoverflow.com/questions/10544887/rotating-a-cgimage – Spads

回答

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平局。在背景石英芯使用甚至更好,该方法优化和表现大大。所以不需要用旋转,平移和缩放来拉你的头发。如果图像已经有方向,也不存在问题。

+0

非常感谢你。你救了我的一天:)这是结果 – HideCode