1

I am working on a side project that it needs to draw on whatever it's displaying on the current screen. However, UIGraphicsBeginImageContextWithOptions only gets me the content of that view only but the entire screen. The drawing view contains many more UI elements and those are not getting drawn. I am just wondering how I should handle it.

Here is how I used to get the current context(self is an instance of UIView):

UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [UIScreen mainScreen].scale);
Lucas Huang
  • 3,998
  • 3
  • 20
  • 29

1 Answers1

2

You first need to have a UIView that covers the entire screen. Then you can draw in the entire UIView. Which UIView are you using now?

To create a UIView that covers the entire screen, you should either add a subview to the main window ([UIApplication sharedApplication].keyWindow) or if you're in a navigationController add a view as overlay to the view hierarchy ([self.navigationController.view addSubview:overlayView]). See this post

Community
  • 1
  • 1
dirkgroten
  • 20,112
  • 2
  • 29
  • 42
  • I was thinking it too complicated! I was trying capture the whole screen and draw it inside the `drawRect`. Your way makes more sense. Most importantly, it's efficient. It won't drive the CPU crazy. – Lucas Huang May 02 '16 at 17:34
  • 1
    BTW, `[self.layer renderInContext:UIGraphicsGetCurrentContext()];` can do the trick. But, it's just used for capturing the screen. Worse, it's not efficient. – Lucas Huang May 02 '16 at 17:37