Trying to make tweak for iOS that takes a screenshot when the device is shaken

60 Views Asked by At

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!

1

There are 1 best solutions below

0
Vlad Z. On

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();