質問編集履歴

1

resizeのメソッドです。

2018/08/14 17:31

投稿

kou009
kou009

スコア16

test CHANGED
File without changes
test CHANGED
@@ -218,6 +218,40 @@
218
218
 
219
219
  }
220
220
 
221
+
222
+
223
+ extension UIImage {
224
+
225
+ func resize(size _size: CGSize) -> UIImage? {
226
+
227
+ let widthRatio = _size.width / size.width
228
+
229
+ let heightRatio = _size.height / size.height
230
+
231
+ let ratio = widthRatio < heightRatio ? widthRatio : heightRatio
232
+
233
+
234
+
235
+ let resizedSize = CGSize(width: size.width * ratio, height: size.height * ratio)
236
+
237
+
238
+
239
+ UIGraphicsBeginImageContextWithOptions(resizedSize, false, 0.0) // 変更
240
+
241
+ draw(in: CGRect(origin: .zero, size: resizedSize))
242
+
243
+ let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
244
+
245
+ UIGraphicsEndImageContext()
246
+
247
+
248
+
249
+ return resizedImage
250
+
251
+ }
252
+
253
+ }
254
+
221
255
  ```
222
256
 
223
257