How to rotate image in Swift?

how to rotate image in ios programmatically
swift rotate image animation infinite
rotate button image swift
swift rotate image 180 degrees
transform uiimage swift
uiimageview rotate
swift rotate camera image
uiimage fix orientation swift 4

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(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

  }

below is the code i am not sure about

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))

This is an extension of UIImage that targets Swift 4.0 and can rotate just the image without the need for a UIImageView. Tested successfully that the image was rotated, and not just had its exif data changed.

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.y, y: -origin.x,
                            width: size.width, height: size.height))
            let rotatedImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()

            return rotatedImage ?? self
        }

        return self
    }
}

To perform a 180 degree rotation, you can call it like this:

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

If for whatever reason it fails to rotate, the original image will then be returned.

Rotating UIImage in Swift, Swift 5 Solution. I wrote Pixel SDK which offers a more powerful solution to this problem, otherwise here is the simplest solution: extension UIImage { func  Fully updated for Xcode 11.5. SwiftUI’s rotationEffect () modifier lets us rotate views freely, using either degrees or radians. For example, if you wanted to rotate some text by -90 degrees so that it reads upwards, you would use this: Text("Up we go") .rotationEffect(.degrees(-90))

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˚

Rotate Image iOS Tutorial, Enter Swift as Language and choose Next. View fullsize. rotate-image-project.​png. extension UIImage { func imageRotated(by degrees: CGFloat) -> UIImage { let orientation = CGImagePropertyOrientation(imageOrientation) // Create CIImage respecting image's orientation guard let inputImage = CIImage(image: self)?.oriented(orientation) else { return self } // Rotate the image itself let rotation = CGAffineTransform(rotationAngle: (degrees * CGFloat.pi / 180)) let outputImage = inputImage.transformed(by: rotation) // Create CGImage first guard let cgImage = CIContext

In one line:

rotatedViewBox.transform = CGAffineTransform(rotationAngle: CGFloat.pi/2)

How to rotate an image in imageview by an angle on iOS App using , How to rotate an image in imageview by an angle on iOS App using Swift? iOS​Apps/ApplicationsMobile Development. Images are everywhere  Answers: set ImageView image. ImageView.transform = ImageView.transform.rotated (by: CGFloat (M_PI_2)) Questions: Answers: let angle = CGFloat (M_PI_2) let tr = CGAffineTransform.identity.rotated (by: angle) ImageView.transform = tr. Questions: Answers: You can use the code below for Swift 3:

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

How to scale, stretch, move, and rotate UIViews , Rotate UIImage by radians in Swift 3. UIImage+Rotation.swift. extension UIImage {. func image(withRotation radians: CGFloat) -> UIImage {. let cgImage = self. Questions: I’m using Xcode 6.0.1 with Swift. I have a UIImage, and I would like to make another image using the old image as a source, with the new image being rotated in some way… say flipped vertically. This question was already answered a few months ago. However, that solution doesn’t work for me, even

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))
})

Rotate UIImage by radians in Swift 3 · GitHub, Rotate the image context. CGContextRotateCTM(bitmap, degreesToRadians(​degrees));. // Now, draw the rotated/scaled image into the context. Here is code to rotate imageview. swift 3.0. UIView.animate(withDuration:2.0, animations: { self.dropDownImage.transform = CGAffineTransform(rotationAngle: CGFloat(imageRotation)) }) Follow this answer to receive notifications. |.

UIImage+Rotation.swift · GitHub, When UIImage loads a photo shot in portrait orientation, it automatically applies a 90° rotation before displaying the image data, and the image's imageOrientation​  extension UIImage { func imageRotated(by degrees: CGFloat) -> UIImage { let orientation = CGImagePropertyOrientation(imageOrientation) // Create CIImage respecting image's orientation guard let inputImage = CIImage(image: self)?.oriented(orientation) else { return self } // Flip the image itself let flip = CGAffineTransform(scaleX: 1, y: -1) let outputImage = inputImage.transformed(by: flip) // Create CGImage first guard let cgImage = CIContext().createCGImage(outputImage, from: outputImage

UIImage.Orientation, This blog is providing how to rotate image using animation. here we are using UIView animation with CGAffineTransform. code is programmatically. Full Code  I want to add two images to single image view (i.e for landscape one image and for portrait another image)but i don't know how to detect orientation changes using swift languages. I tried this answer but it takes only one image

Rotate Image Animation Swift 5., This is an extension of UIImage that targets Swift 4.0 and can rotate just the image without the need for a UIImageView. Tested successfully that the image was  in swift there is a Gesture we used for Rotation, pan, tap, longpress pinch, swipe So with used of Rotation gesture method you will able to rotate a image and pinch gesture method you will zoom image. 2.4k views · View 1 Upvoter

Comments
  • Manage the condition for image after capture from camera. And try this link: stackoverflow.com/questions/9324130/… It will help to sort out from the issue.
  • stackoverflow.com/a/33479054/291240
  • When getting errors, never forget to tell people which ones they are!
  • This works, however the translation/origin seem to be wrong. The rotated image is shifted up and to the left :-/
  • @lenooh Can you post details on how you are calling it and some general details on the image?
  • CodeBender: I'm calling it with let rotatedImage = image.rotate(radians: .pi). The image is 1433 × 2099. I then found another solution which works perfectly: stackoverflow.com/a/47402811/529663
  • As @lenooh points out. The accepted solution has several translation issues, The solution that seems to work for everyone is posted in his comment. Here again for your convenience: stackoverflow.com/a/47402811/529663
  • Why M_PI_4? 90 degrees = M_PI_2
  • @JayeshMiruliya, also in Swift 3 you can use CGFloat.pi constant (divided by 2 ofc)
  • For Swift 4 Use: .pi/1 for 180˚ and .pi/2 for 90˚
  • To rotate UIImage you may check this GitHub link. Working for Swift3. You may use ".pi/1 for 180˚ and .pi/2 for 90˚" for radians param as per above comment. gist.github.com/norsez/e318d68f6a48786d35b3e7fd3b3a32ae
  • For animation - UIView.animate(withDuration: 3) { self.needleImageView.transform = self.needleImageView.transform.rotated(by: CGFloat(Double.pi/2)) }