簡體   English   中英

Swift-圖像旋轉度數-變形結果

[英]Swift - Image rotation in degrees - deformed result

我發現有幾種方法可以在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度而圖像大小發生了改變。

輪換之前:

旋轉前的圖像

后續旋轉(缺少菜單,因為它已自動隱藏):

旋轉后的圖像

奇怪的是,當我用人像拍照時會發生這種情況。 當我在橫向拍照時,旋轉效果很好(即使我嘗試旋轉幾次,即使我從橫向旋轉到肖像,然后再旋轉到橫向,它仍然可以正常工作)。

我看到了一些有關自動布局約束問題的討論,但我不確定這是否是我的問題,以及為什么在某些情況下會起作用而在某些情況下不會起作用。 我在集合單元格中有照片,每個單元格都有第一個UIScrollView,然后在UIScrollView中有帶照片的UIImageView。 所有約束均設置為界限。

如果有幫助,我只想旋轉90或-90度。 這可能會使方法更簡單,並且修復起來更容易。

謝謝你的幫助。

實際上,最簡單且可能最好的方法是創建視圖,在其上放置圖像視圖,旋轉圖像視圖,調整超級視圖的大小並創建該視圖的屏幕快照。

您實際上並沒有在視圖層次結構中添加這些視圖,而只是使用它們來輕松生成圖像。

之所以這樣做是一個好程序,是因為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將更改圖像方向,然后將其用於表示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM