質問編集履歴

2

エラーメッセージの追加

2019/11/12 11:18

投稿

shuhei-cyf
shuhei-cyf

スコア15

test CHANGED
File without changes
test CHANGED
@@ -44,6 +44,8 @@
44
44
 
45
45
  ###追記
46
46
 
47
+ 更新したコード
48
+
47
49
 
48
50
 
49
51
  ```Swift
@@ -81,3 +83,13 @@
81
83
  }
82
84
 
83
85
  ```
86
+
87
+
88
+
89
+ エラーメッセージ
90
+
91
+ ```ここに言語を入力
92
+
93
+ [Unknown process name] CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
94
+
95
+ ```

1

コードの変更

2019/11/12 11:18

投稿

shuhei-cyf
shuhei-cyf

スコア15

test CHANGED
File without changes
test CHANGED
@@ -39,3 +39,45 @@
39
39
 
40
40
 
41
41
  heightを1/1.3にして試してみたのですが、これだとスクショが上寄りになってしまうようです。同じ高さで画面の中央を撮ることは可能でしょうか?
42
+
43
+
44
+
45
+ ###追記
46
+
47
+
48
+
49
+ ```Swift
50
+
51
+ func takeScreenShot() {
52
+
53
+
54
+
55
+ let size = CGSize(width: CGFloat(UIScreen.main.bounds.size.width), height: CGFloat(UIScreen.main.bounds.size.height/1.3))
56
+
57
+ let rect = CGRect(origin: CGPoint.zero, size: size)
58
+
59
+ let height = UIScreen.main.bounds.size.height
60
+
61
+ // 上下の余白が同じサイズになる値を計算
62
+
63
+ let startWith = (height - (height/1.3)) / 2
64
+
65
+ if let context = UIGraphicsGetCurrentContext() {
66
+
67
+ context.translateBy(x: 0, y: -startWith)
68
+
69
+ context.fill(rect)
70
+
71
+ }
72
+
73
+
74
+
75
+ self.view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
76
+
77
+ screenShotImage = UIImage(cgImage: (UIGraphicsGetCurrentContext()?.makeImage())!)
78
+
79
+ UIGraphicsEndImageContext()
80
+
81
+ }
82
+
83
+ ```