質問編集履歴

1

コードの追加

2019/01/16 10:39

投稿

mimamo
mimamo

スコア44

test CHANGED
File without changes
test CHANGED
@@ -124,7 +124,103 @@
124
124
 
125
125
  ```
126
126
 
127
-
127
+ ###頂いたコードを参考に記述したもの
128
+
129
+ ```Swift
130
+
131
+ extension CGRect {
132
+
133
+ func aspectFit(contentSize: CGSize, stretchble: Bool, integer: Bool) -> CGRect {
134
+
135
+ let xZoom = width / contentSize.width
136
+
137
+ let yZoom = height / contentSize.height
138
+
139
+ let zoom = stretchble ? min(xZoom, yZoom) : min(xZoom, yZoom, 1)
140
+
141
+
142
+
143
+ if integer {
144
+
145
+ let newWidth = max(Int(contentSize.width * zoom), 1)
146
+
147
+ let newHeight = max(Int(contentSize.height * zoom), 1)
148
+
149
+ let newX = Int(origin.x) + (Int(width) - newWidth) / 2
150
+
151
+ let newY = Int(origin.y) + (Int(height) - newHeight) / 2
152
+
153
+ return CGRect(x: newX, y: newY, width: newWidth, height: newHeight)
154
+
155
+ } else {
156
+
157
+ let newWidth = contentSize.width * zoom
158
+
159
+ let newHeight = contentSize.height * zoom
160
+
161
+ let newX = origin.x + (width - newWidth) / 2
162
+
163
+ let newY = origin.y + (height - newHeight) / 2
164
+
165
+ return CGRect(x: newX, y: newY, width: newWidth, height: newHeight)
166
+
167
+ }
168
+
169
+ }
170
+
171
+ }
172
+
173
+
174
+
175
+
176
+
177
+
178
+
179
+ // 写真を選んだ後に呼ばれる処理
180
+
181
+ func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
182
+
183
+ // 選択した写真を取得する
184
+
185
+ let image = info[UIImagePickerControllerOriginalImage] as! UIImage
186
+
187
+
188
+
189
+ //透過していた画像を取り戻す
190
+
191
+ getphoto.alpha = 1.0
192
+
193
+
194
+
195
+
196
+
197
+
198
+
199
+
200
+
201
+
202
+
203
+
204
+
205
+ getphoto.frame = CGRect(x: 0, y: 0, width: screenWidth / 4, height: screenHeight / 4).aspectFit(contentSize: image.size, stretchble: true, integer: true)
206
+
207
+ getphoto.center = CGPoint(x:screenWidth/2, y:screenHeight/2)
208
+
209
+
210
+
211
+
212
+
213
+ // ビューに表示する
214
+
215
+ self.getphoto.image = image
216
+
217
+ // 写真を選ぶビューを引っ込める
218
+
219
+ self.dismiss(animated: true)
220
+
221
+ }
222
+
223
+ ```
128
224
 
129
225
 
130
226