teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

追記

2020/06/07 12:15

投稿

tsuki01
tsuki01

スコア1751

answer CHANGED
@@ -48,4 +48,102 @@
48
48
  return newImage
49
49
  }
50
50
  }
51
- ```
51
+ ```
52
+
53
+ ---
54
+
55
+ ### 2020.06.07追記
56
+ 保存時のサイズ変更(ImageViewの余白部分削除)に関して追記いたします。
57
+
58
+ 以下の参考サイトの方法は使えませんでしょうか。
59
+ **※どちらも表示時から余白を削除するものなので”保存時”とは異なります。
60
+ また、トリミング時に多少の誤差(数px)が生じるかもしれません。
61
+ もし問題あれば、別質問とした方が良い回答が得られるかもしれません(お役に立てず申し訳ありませんが。。)**
62
+ - [How to find an aspect fit image’s size inside an image view](https://www.hackingwithswift.com/example-code/uikit/how-to-find-an-aspect-fit-images-size-inside-an-image-view)
63
+ - [UIImageViewを、カメラロールから取得した画像のアスペクト比の形にしたい](https://teratail.com/questions/169060)
64
+
65
+ **以下サンプルです(上記で1つめに挙げた参考サイトの方法を利用)**
66
+ ポイントとしては、「extentionの追加」と「self.imageView.frame = self.imageView.contentClippingRect」の部分となります。
67
+
68
+ ```Swift
69
+ import UIKit
70
+
71
+ class ViewController: UIViewController {
72
+
73
+ @IBOutlet weak var imageView: UIImageView! // 合成対象のImageView
74
+ @IBOutlet weak var lbl: UILabel! // 合成対象のLabel
75
+ @IBOutlet weak var resultImageView: UIImageView!// 合成結果表示用のImageView
76
+
77
+
78
+ override func viewDidLoad() {
79
+ super.viewDidLoad()
80
+
81
+ // LabelをImageView上に追加する
82
+ imageView.addSubview(lbl)
83
+
84
+ // 結果表示用ImageViewの枠線設定
85
+ resultImageView.layer.borderWidth = 2
86
+ resultImageView.layer.borderColor = UIColor.red.cgColor
87
+ }
88
+
89
+
90
+ override func viewDidAppear(_ animated: Bool) {
91
+ super.viewDidAppear(animated)
92
+
93
+ // ImageViewから余白削除
94
+ self.imageView.frame = self.imageView.contentClippingRect
95
+
96
+ // ImageViewとLabelを合成し、結果表示用のImageViewにImage設定
97
+ self.resultImageView.image = self.takeScreenShot()
98
+ }
99
+
100
+ // Viewの合成処理(キャプチャ取得処理)
101
+ private func takeScreenShot() -> UIImage {
102
+
103
+ // コンテキスト開始
104
+ UIGraphicsBeginImageContextWithOptions(self.imageView.bounds.size, false, 0.0)
105
+
106
+ // ImageView(ImageView上のView含む)を書き出す
107
+ self.imageView.drawHierarchy(in: self.imageView.bounds, afterScreenUpdates: true)
108
+
109
+ // newImageに、コンテキストの内容を書き出す
110
+ let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
111
+
112
+ // コンテキストを閉じる
113
+ UIGraphicsEndImageContext()
114
+
115
+ // Imageを返却
116
+ return newImage
117
+ }
118
+ }
119
+
120
+ // 参考サイトのextension定義
121
+ extension UIImageView {
122
+ var contentClippingRect: CGRect {
123
+ guard let image = image else { return bounds }
124
+ guard contentMode == .scaleAspectFit else { return bounds }
125
+ guard image.size.width > 0 && image.size.height > 0 else { return bounds }
126
+
127
+ let scale: CGFloat
128
+ if image.size.width > image.size.height {
129
+ scale = bounds.width / image.size.width
130
+ } else {
131
+ scale = bounds.height / image.size.height
132
+ }
133
+
134
+ let size = CGSize(width: image.size.width * scale, height: image.size.height * scale)
135
+ let x = (bounds.width - size.width) / 2.0
136
+ let y = (bounds.height - size.height) / 2.0
137
+
138
+ return CGRect(x: x, y: y, width: size.width, height: size.height)
139
+ }
140
+ }
141
+
142
+ ```
143
+
144
+
145
+ **余白削除前(分かりやすい様に、ImageViewの背景を青色に設定)**
146
+ ![Before](4a5eefa34cb9613b3651e531973e8513.png)
147
+
148
+ **余白削除後**
149
+ ![After](247721f69ad45b3e85067f4b7f3f058f.png)