Last active
February 19, 2023 04:54
-
-
Save norsez/e318d68f6a48786d35b3e7fd3b3a32ae to your computer and use it in GitHub Desktop.
Rotate UIImage by radians in Swift 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UIImage { | |
func image(withRotation radians: CGFloat) -> UIImage { | |
let cgImage = self.cgImage! | |
let LARGEST_SIZE = CGFloat(max(self.size.width, self.size.height)) | |
let context = CGContext.init(data: nil, width:Int(LARGEST_SIZE), height:Int(LARGEST_SIZE), bitsPerComponent: cgImage.bitsPerComponent, bytesPerRow: 0, space: cgImage.colorSpace!, bitmapInfo: cgImage.bitmapInfo.rawValue)! | |
var drawRect = CGRect.zero | |
drawRect.size = self.size | |
let drawOrigin = CGPoint(x: (LARGEST_SIZE - self.size.width) * 0.5,y: (LARGEST_SIZE - self.size.height) * 0.5) | |
drawRect.origin = drawOrigin | |
var tf = CGAffineTransform.identity | |
tf = tf.translatedBy(x: LARGEST_SIZE * 0.5, y: LARGEST_SIZE * 0.5) | |
tf = tf.rotated(by: CGFloat(radians)) | |
tf = tf.translatedBy(x: LARGEST_SIZE * -0.5, y: LARGEST_SIZE * -0.5) | |
context.concatenate(tf) | |
context.draw(cgImage, in: drawRect) | |
var rotatedImage = context.makeImage()! | |
drawRect = drawRect.applying(tf) | |
rotatedImage = rotatedImage.cropping(to: drawRect)! | |
let resultImage = UIImage(cgImage: rotatedImage) | |
return resultImage | |
} | |
} |
Yes you can. You have my permission. Please use it.
On Mon, Mar 14, 2022 at 2:00 PM seyoung.hyun ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
@norsez <https://github.com/norsez> Thanks your code :)
Can I use your code in my commercial app?
Please let me know how I can use the code for commercial distribution.
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/e318d68f6a48786d35b3e7fd3b3a32ae#gistcomment-4096835>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AANNHFJXLGVFAXIB7TUSEIDU73PZRANCNFSM5QUTKOLQ>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
You are receiving this because you were mentioned.Message ID:
***@***.***>
--
Sent from Gmail Mobile
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@norsez Thanks your code :)
Can I use your code in my commercial app?
Please let me know how I can use the code for commercial distribution.