How to rotate image in Swift?

后端 未结 13 1216
萌比男神i
萌比男神i 2020-11-30 03:55

I am unable to rotate the image by 90 degrees in swift. I have written below code but there is an error and doesn\'t compile

  func imageRotatedByDegrees(old         


        
相关标签:
13条回答
  • 2020-11-30 04:11

    set ImageView image

    ImageView.transform = ImageView.transform.rotated(by: CGFloat(M_PI_2))
    

    Swift 5

    ImageView.transform = ImageView.transform.rotated(by: .pi)        // 180˚
    
    ImageView.transform = ImageView.transform.rotated(by: .pi / 2)    // 90˚
    
    ImageView.transform = ImageView.transform.rotated(by: .pi * 1.5)  // 270˚
    
    0 讨论(0)
  • 2020-11-30 04:13

    In Swift 4.2 and Xcode 10.0

    dropDownImageView.transform = dropDownImageView.transform.rotated(by: CGFloat(Double.pi / 2))
    

    If you want to add animation...

    UIView.animate(withDuration: 2.0, animations: {
       self.dropDownImageView.transform = self.dropDownImageView.transform.rotated(by: CGFloat(Double.pi / 2))
    })
    
    0 讨论(0)
  • 2020-11-30 04:13

    You can first set your image in imageView and rotate get your image in imageView

    Here:

    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 讨论(0)
  • 2020-11-30 04:15

    If you just don't want specific rotation and like to use all orientations one by one.

    func rotateImage() -> UIImage {
        let newOrientation = Orientation(rawValue: self.imageOrientation.rawValue + 1) ?? UIImage.Orientation(rawValue: 0)
        return UIImage(cgImage: self.cgImage!, scale: self.scale, orientation: newOrientation!)
    }
    
    0 讨论(0)
  • 2020-11-30 04:20

    You can use the code below for Swift 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
    }
    
    0 讨论(0)
  • 2020-11-30 04:21

    In one line:

    rotatedViewBox.transform = CGAffineTransform(rotationAngle: CGFloat.pi/2)
    
    0 讨论(0)
提交回复
热议问题