Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
1
Help us understand the problem. What is going on with this article?
@Nao9syu

UIImageを縦横回転させる

More than 1 year has passed since last update.

はじめに

Exifを使わない状態でUIImageを縦横ごと回転させる。
例えば、W288*H352をW352*H288かつExifなしに変換したい。
検索すれば回転自体はたくさん出てくるが、
縦横は変わらないものしか見つからなかったため、自分で書き直した。

参考

【Swift】切り抜かずにUIImageを回転させる

Swift - UIImageをCoreGraphicsで回転させる

ソース(Swift5)

rotate.swift

extension UIImage {
    func rotatedBy(degree: CGFloat) -> UIImage {
        let w = self.size.width
        let h = self.size.height

        //写し先を準備
        let s = CGSize(width: h, height: w)
        UIGraphicsBeginImageContext(s)
        let context = UIGraphicsGetCurrentContext()!
        //中心点
        context.translateBy(x: h / 2, y: w / 2)
        //Y軸を反転させる
        context.scaleBy(x: 1.0, y: -1.0)

        //回転させる
        let radian = -degree * CGFloat.pi / 180
        context.rotate(by: radian)

        //書き込み
        let rect = CGRect(x: -(h / 2), y: -(w / 2), width: h, height: w)
        context.draw(self.cgImage!, in: rect)

        let rotatedImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return rotatedImage
    }
}

使い方

uiimage = uiimage.rotate(0) //反時計回りに90度回転
uiimage = uiimage.rotate(180) //時計回りに90度回転

1
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Nao9syu
言語:VB.NET,C#,C++,HTML,CSS,JavaScript。 道具:Windows,Webカメラ,THETA。 資格:基本,応用,セスぺ(ラスト),DBスペ。 領域:Webソフト,Winソフト,Winサーバー,RDB,生産品質管理の統計,コンピュータビジョン。

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
1
Help us understand the problem. What is going on with this article?