2016-11-30 120 views
19

我unbale將圖像旋轉到迅速3.I 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(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, in: CGRect(origin: (x: -oldImage.size.width/2, y: -oldImage.size.height/2, width: oldImage.size.width, height: oldImage.size.height), size: oldImage.cgImage)) 



    let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()! 
    UIGraphicsEndImageContext() 
    return newImage 
    } 
下面的代碼

我不能夠寫&不知道什麼是它

bitmap.draw(oldImage, in: CGRect(origin: (x: -oldImage.size.width/2, y: -oldImage.size.height/2, width: oldImage.size.width, height: oldImage.size.height), size: oldImage.cgImage)) 
+0

http:// stackoverflow。com/a/33479054/291240 –

+2

當出現錯誤時,請不要忘記告訴人們他們是誰! –

+0

從相機捕捉後管理圖像的條件。並嘗試此鏈接:http://stackoverflow.com/questions/9324130/iphone-image-captured-from-camera-rotate-90-degree-automatically它將有助於理清問題。 –

回答

8
let angle = CGFloat(M_PI_2) 
    let tr = CGAffineTransform.identity.rotated(by: angle) 
    ImageView.transform = tr 
+5

我想旋轉UIImage不在imageview – Techiee

25

集的ImageView圖像

ImageView.transform = ImageView.transform.rotated(by: CGFloat(M_PI_2)) 
問題
+2

爲什麼M_PI_4? 90度= M_PI_2 – alexburtnik

+1

好的......謝謝@alexburtnik –

+3

@JayeshMiruliya,也可以在Swift 3中使用'CGFloat.pi'常量(除以2 ofc) – user28434

0

你的代碼與功能並不遙遠。您可以將轉化爲你做直接對位,你並不需要一箇中間的看法:

func imageRotatedByDegrees(oldImage: UIImage, deg degrees: CGFloat) -> UIImage { 
    let size = oldImage.size 

    UIGraphicsBeginImageContext(size) 

    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: size.width/2, y: size.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) 

    let origin = CGPoint(x: -size.width/2, y: -size.width/2) 

    bitmap.draw(oldImage.cgImage!, in: CGRect(origin: origin, size: size)) 

    let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()! 
    UIGraphicsEndImageContext() 
    return newImage 
} 

另外,如果你要創建一個旋轉圖像的功能,這是典型的好形式以包括clockwise: Bool參數,該參數將解釋degrees參數爲順時針旋轉或不旋轉。實施和適當的轉換爲弧度我留給你。

另請注意,假設oldImage.size不爲零,我有點手動波動。如果是這樣,解壓UIGraphicsGetCurrentContext()!可能會崩潰。您應驗證oldImage的尺寸,如果無效,則返回oldImage

+0

雖然功能,此代碼不會旋轉中心的圖像,除非在特殊情況下圖像是方形的。我使用了中間方法,它產生了大約中心旋轉。 –

0

您的問題是您使用CGRect的不存在的初始值設定項。如果你想使用CGRect(來源:大小:),然後創建原點,沒有寬度和高度參數。或者移除尺寸參數並使用CGRect(x:y:width:height :)

只是這一個替換此行

bitmap.draw(oldImage, in: CGRect(origin: (x: -oldImage.size.width/2, y: -oldImage.size.height/2, width: oldImage.size.width, height: oldImage.size.height), size: oldImage.cgImage)) 

let rect = CGRect(origin: CGPoint(x: -oldImage.size.width/2, y: -oldImage.size.height/2), size: oldImage.size) 
bitmap.draw(oldImage.cgImage!, in: rect) 
6

您可以使用下面的代碼斯威夫特3:

extension UIImage { 

func fixImageOrientation() -> UIImage? { 
    var flip:Bool = false //used to see if the image is mirrored 
    var isRotatedBy90:Bool = false // used to check whether aspect ratio is to be changed or not 

    var transform = CGAffineTransform.identity 

    //check current orientation of original image 
    switch self.imageOrientation { 
    case .down, .downMirrored: 
     transform = transform.rotated(by: CGFloat(M_PI)); 

    case .left, .leftMirrored: 
     transform = transform.rotated(by: CGFloat(M_PI_2)); 
     isRotatedBy90 = true 
    case .right, .rightMirrored: 
     transform = transform.rotated(by: CGFloat(-M_PI_2)); 
     isRotatedBy90 = true 
    case .up, .upMirrored: 
     break 
    } 

    switch self.imageOrientation { 

    case .upMirrored, .downMirrored: 
     transform = transform.translatedBy(x: self.size.width, y: 0) 
     flip = true 

    case .leftMirrored, .rightMirrored: 
     transform = transform.translatedBy(x: self.size.height, y: 0) 
     flip = true 
    default: 
     break; 
    } 

    // calculate the size of the rotated view's containing box for our drawing space 
    let rotatedViewBox = UIView(frame: CGRect(origin: CGPoint(x:0, y:0), size: size)) 
    rotatedViewBox.transform = transform 
    let rotatedSize = rotatedViewBox.frame.size 

    // Create the bitmap context 
    UIGraphicsBeginImageContext(rotatedSize) 
    let bitmap = 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.0, y: rotatedSize.height/2.0); 

    // Now, draw the rotated/scaled image into the context 
    var yFlip: CGFloat 

    if(flip){ 
     yFlip = CGFloat(-1.0) 
    } else { 
     yFlip = CGFloat(1.0) 
    } 

    bitmap!.scaleBy(x: yFlip, y: -1.0) 

    //check if we have to fix the aspect ratio 
    if isRotatedBy90 { 
     bitmap?.draw(self.cgImage!, in: CGRect(x: -size.width/2, y: -size.height/2, width: size.height,height: size.width)) 
    } else { 
     bitmap?.draw(self.cgImage!, in: CGRect(x: -size.width/2, y: -size.height/2, width: size.width,height: size.height)) 
    } 

    let fixedImage = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 

    return fixedImage 
} 
+1

完美,謝謝 –

2

您可以先設置你的圖像中imageView和旋轉讓你的圖像在圖像查看

這裏:

let customImageView = UIImageView() 
customImageView.frame = CGRect.init(x: 0, y: 0, w: 250, h: 250) 
customImageView.image = UIImage(named: "yourImage") 

customImageView .transform = customImageView .transform.rotated(by: CGFloat.init(M_PI_2)) 
var rotatedImage = UIImage() 
rotatedImage = customImageView.image! 
0

這是UIImage的擴展,針對雨燕4.0,可以無需一個UIImageView只是旋轉圖像。測試成功,圖像旋轉,而不僅僅是其exif數據更改。

import UIKit 

extension UIImage { 
    func rotate(radians: CGFloat) -> UIImage { 
     let rotatedSize = CGRect(origin: .zero, size: size).applying(CGAffineTransform(rotationAngle: CGFloat(radians))).integral.size 
     UIGraphicsBeginImageContext(rotatedSize) 
     if let context = UIGraphicsGetCurrentContext() { 
      let origin = CGPoint(x: rotatedSize.width/2.0, y: rotatedSize.height/2.0) 
      context.translateBy(x: origin.x, y: origin.y) 
      context.rotate(by: radians) 
      draw(in: CGRect(x: -origin.x, y: -origin.y, width: size.width, height: size.height)) 
      let rotatedImage = UIGraphicsGetImageFromCurrentImageContext() 
      UIGraphicsEndImageContext() 

      return rotatedImage ?? self 
     } 

     return self 
    } 
} 

要進行180度旋轉,你可以這樣調用:

let rotatedImage = image.rotate(radians: .pi) 

如果出於某種原因,它不能旋轉,原始圖像將被退回。