質問編集履歴
1
修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -6,32 +6,4 @@
|
|
6
6
|
※ 20KB以下にしたいのですが、1029KB => 214KBが限界でした。
|
7
7
|
|
8
8
|
どのようにすれば、それ以上の圧縮ができるのでしょうか。
|
9
|
-
それともiOS側での処理を諦め、サーバーサイドでの処理が必要になるのでしょうか。
|
9
|
+
それともiOS側での処理を諦め、サーバーサイドでの処理が必要になるのでしょうか。
|
10
|
-
|
11
|
-
```
|
12
|
-
static func convertToJPEG(image: UIImage, maxBytes: Int = 20000) -> Data? {
|
13
|
-
var quality: CGFloat = 0.7
|
14
|
-
guard var data = UIImageJPEGRepresentation(image, quality) else {
|
15
|
-
return nil
|
16
|
-
}
|
17
|
-
|
18
|
-
while data.count > maxBytes {
|
19
|
-
quality *= min(0.7, CGFloat(maxBytes) / CGFloat(data.count))
|
20
|
-
guard let newData = UIImageJPEGRepresentation(image, quality) else {
|
21
|
-
return nil
|
22
|
-
}
|
23
|
-
|
24
|
-
if data.count == newData.count && quality != 0 {
|
25
|
-
quality = 0
|
26
|
-
}
|
27
|
-
|
28
|
-
data = newData
|
29
|
-
|
30
|
-
if quality == 0 {
|
31
|
-
break
|
32
|
-
}
|
33
|
-
}
|
34
|
-
|
35
|
-
return data
|
36
|
-
}
|
37
|
-
```
|