Rotate UIImage in swift

后端 未结 8 1442
一整个雨季
一整个雨季 2021-02-04 10:35

I want to rotate UIImage in clockwise direction. But my current code do not perform function accurate some time it rotate and some time its skip rotation. I want my UIImage will

相关标签:
8条回答
  • 2021-02-04 11:02

    I believe it's worth to post solution using CoreImage. It's just couple of lines and it works great for me.

    Please note: when getting final UIImage, it's necessary to convert to CGImage first to respect extent of CIImage

    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().createCGImage(outputImage, from: outputImage.extent) 
                else { return self }
    
            // Create output UIImage from CGImage
            return UIImage(cgImage: cgImage)
        }
    }
    
    0 讨论(0)
  • 2021-02-04 11:07

    This works for me:

    works Fine :)

    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: CGRectMake(0, 0, oldImage.size.width, oldImage.size.height))
        let t: CGAffineTransform = CGAffineTransformMakeRotation(degrees * CGFloat(M_PI / 180))
        rotatedViewBox.transform = t
        let rotatedSize: CGSize = rotatedViewBox.frame.size
        //Create the bitmap context
        UIGraphicsBeginImageContext(rotatedSize)
        let bitmap: CGContextRef = UIGraphicsGetCurrentContext()!
        //Move the origin to the middle of the image so we will rotate and scale around the center.
        CGContextTranslateCTM(bitmap, rotatedSize.width / 2, rotatedSize.height / 2)
        //Rotate the image context
        CGContextRotateCTM(bitmap, (degrees * CGFloat(M_PI / 180)))
        //Now, draw the rotated/scaled image into the context
        CGContextScaleCTM(bitmap, 1.0, -1.0)
        CGContextDrawImage(bitmap, CGRectMake(-oldImage.size.width / 2, -oldImage.size.height / 2, oldImage.size.width, oldImage.size.height), oldImage.CGImage)
        let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return newImage
    }
    

    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 讨论(0)
提交回复
热议问题