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

質問編集履歴

2

エラーメッセージの追加

2019/11/12 11:18

投稿

shuhei-cyf
shuhei-cyf

スコア15

title CHANGED
File without changes
body CHANGED
@@ -21,6 +21,7 @@
21
21
  heightを1/1.3にして試してみたのですが、これだとスクショが上寄りになってしまうようです。同じ高さで画面の中央を撮ることは可能でしょうか?
22
22
 
23
23
  ###追記
24
+ 更新したコード
24
25
 
25
26
  ```Swift
26
27
  func takeScreenShot() {
@@ -39,4 +40,9 @@
39
40
  screenShotImage = UIImage(cgImage: (UIGraphicsGetCurrentContext()?.makeImage())!)
40
41
  UIGraphicsEndImageContext()
41
42
  }
43
+ ```
44
+
45
+ エラーメッセージ
46
+ ```ここに言語を入力
47
+ [Unknown process name] CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
42
48
  ```

1

コードの変更

2019/11/12 11:18

投稿

shuhei-cyf
shuhei-cyf

スコア15

title CHANGED
File without changes
body CHANGED
@@ -18,4 +18,25 @@
18
18
  }
19
19
  ```
20
20
 
21
- heightを1/1.3にして試してみたのですが、これだとスクショが上寄りになってしまうようです。同じ高さで画面の中央を撮ることは可能でしょうか?
21
+ heightを1/1.3にして試してみたのですが、これだとスクショが上寄りになってしまうようです。同じ高さで画面の中央を撮ることは可能でしょうか?
22
+
23
+ ###追記
24
+
25
+ ```Swift
26
+ func takeScreenShot() {
27
+
28
+ let size = CGSize(width: CGFloat(UIScreen.main.bounds.size.width), height: CGFloat(UIScreen.main.bounds.size.height/1.3))
29
+ let rect = CGRect(origin: CGPoint.zero, size: size)
30
+ let height = UIScreen.main.bounds.size.height
31
+ // 上下の余白が同じサイズになる値を計算
32
+ let startWith = (height - (height/1.3)) / 2
33
+ if let context = UIGraphicsGetCurrentContext() {
34
+ context.translateBy(x: 0, y: -startWith)
35
+ context.fill(rect)
36
+ }
37
+
38
+ self.view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
39
+ screenShotImage = UIImage(cgImage: (UIGraphicsGetCurrentContext()?.makeImage())!)
40
+ UIGraphicsEndImageContext()
41
+ }
42
+ ```