2017-05-16 11 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

soo를 이용해 주셔서 감사합니다. 당신은 내 하루를 저장 :) 그것은 결과 – HideCode