0

I’m trying to build a simple tweak in objective-c that will take a screenshot when the device is shaken. I’ve gotten half the thing working since I tested the tweak to send an alert when the device is shaken, but I’d like it to take a screenshot instead but I can’t seem to find the proper code.

Here’s the code I have right now:

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if(event.subtype == UIEventSubtypeMotionShake && self == [[UIApplication sharedApplication] keyWindow]) {
     /// Screenshot action should go here. I’ve tried multiple methods but none works.
  }
}

I’ve tried many methods and hooks but nothing worked for the screenshot. I would really appreciate some help!

Vlad Z.
  • 3,401
  • 3
  • 32
  • 60
  • Does this answer your question? [how to take a screenshot of the iPhone programmatically?](https://stackoverflow.com/questions/3610604/how-to-take-a-screenshot-of-the-iphone-programmatically) – Gereon Apr 02 '20 at 19:23

1 Answers1

0

Ideally, you would want to have something like this:

/// Grab current window
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
CGRect rect = [keyWindow bounds];

/// Create new image context with the size of your screen
UIGraphicsBeginImageContextWithOptions(rect.size, YES, 0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
[keyWindow.layer renderInContext:context];   

/// Grab image from current context
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Vlad Z.
  • 3,401
  • 3
  • 32
  • 60