回答編集履歴

1

追記。コード追加

2016/01/31 15:31

投稿

u39ueda
u39ueda

スコア950

test CHANGED
@@ -7,3 +7,103 @@
7
7
  http://temping-amagramer.blogspot.jp/2013/10/iosobjective-cuiimage.html?m=1
8
8
 
9
9
  これとか参考にすればどうでしょう。
10
+
11
+
12
+
13
+ 追記
14
+
15
+ 試してみると想像以上に難しいものでした。
16
+
17
+
18
+
19
+ それにすみません。提示した参考サイトを元に自分でやってみるとうまく反転しませんでした。
20
+
21
+ 多分OSバージョンが違うからかキャプチャの方法が違うからだと思いますが……
22
+
23
+
24
+
25
+ 良さげなURLが見つからなかったので自分で実装してみました。
26
+
27
+ 一応手元で試した限りではうまく動くみたいです。
28
+
29
+
30
+
31
+ ```Swift
32
+
33
+ func flipHorizontal(image: UIImage) -> UIImage {
34
+
35
+ let originalOrientation = image.imageOrientation
36
+
37
+
38
+
39
+ // UIImageに設定されている回転方向を初期化したUIImageを作成.
40
+
41
+ // 'UIImageOrientation.Up'はホームボタンが右手にある状態. これがデフォルトらしい.
42
+
43
+ let landscapeImage = UIImage(CGImage: image.CGImage!, scale: image.scale, orientation: .Up)
44
+
45
+
46
+
47
+ UIGraphicsBeginImageContextWithOptions(landscapeImage.size, false, landscapeImage.scale)
48
+
49
+ let context = UIGraphicsGetCurrentContext()
50
+
51
+
52
+
53
+ // CGContextDrawImageで描画する場合、左下が原点となる.
54
+
55
+ // 扱いやすい右上を原点とするために上下反転する.
56
+
57
+ CGContextTranslateCTM(context, 0, landscapeImage.size.height)
58
+
59
+ CGContextScaleCTM(context, 1.0, -1.0)
60
+
61
+
62
+
63
+ // 左右を反転する.
64
+
65
+ // 元画像が横向きだったらそのまま左右反転でいいが、縦向きだったら90度回転しているので上下反転する.
66
+
67
+ switch originalOrientation {
68
+
69
+ case .Up, .UpMirrored, .Down, .DownMirrored:
70
+
71
+ // 左右反転
72
+
73
+ CGContextTranslateCTM(context, landscapeImage.size.width, 0)
74
+
75
+ CGContextScaleCTM(context, -1.0, 1.0)
76
+
77
+ case .Left, .LeftMirrored, .Right, .RightMirrored:
78
+
79
+ // 上下反転
80
+
81
+ CGContextTranslateCTM(context, 0, landscapeImage.size.height)
82
+
83
+ CGContextScaleCTM(context, 1.0, -1.0)
84
+
85
+ }
86
+
87
+ // 画像を描画
88
+
89
+ CGContextDrawImage(context, CGRect(origin: CGPoint.zero, size: landscapeImage.size), landscapeImage.CGImage)
90
+
91
+ // 画像取得
92
+
93
+ let flipHorizontalImage = UIGraphicsGetImageFromCurrentImageContext()
94
+
95
+
96
+
97
+ UIGraphicsEndImageContext()
98
+
99
+
100
+
101
+ // 最初に無効にしたUIImageの回転を再設定する
102
+
103
+ return UIImage(CGImage: flipHorizontalImage.CGImage!, scale: flipHorizontalImage.scale, orientation: originalOrientation)
104
+
105
+ }
106
+
107
+ ```
108
+
109
+