IOS development-Detects user screenshots and obtains captured images and ios screenshots

Source: Internet
Author: User

IOS development-Detects user screenshots, and obtains the screenshots of all images and ios.

You can detect the user screenshot (Home + Power), and then click the Add function button later to ask if you want to send the screenshot. This user experience is very good. As a result, I also want to implement this function.


Before iOS7, if a screenshot is taken, the system automatically cancels all touch events on the screen.TouchesCancelled: withEvent: This method) Then we can detect the call of this method, load the latest local image, and then judge to achieve our goal. However, after iOS 7, screenshots no longer cancel screen touch events, as a result, apps such as Snapchat and Facebook Poke depend on the system behavior function when iOS 7 is released.

If you do not take any new measures, we can enable the app to cyclically check the latest photo in the album in the background after it is started to see if it meets the screenshot features. This method is feasible, but this is a stupid method. You need to allow your program to access the album, and the loop in the background will consume more system resources.

Of course, if Apple closes some things, it will certainly open others to you, and it will not let you embark on the path.

IOS7 provides a new push method:UIApplicationUserDidTakeScreenshotNotification. You just need to subscribe as usual to know when it will be.
Note: UIApplicationUserDidTakeScreenshotNotificationWill be displayed after completion. You cannot receive a notification before the screenshot is taken.

We hope Apple will add UIApplicationUserWillTakeScreenshotNotification to ios8. (Only did, no will is obviously not an Apple style ...)



Below is a small demo, which detects user screenshots and obtains screenshots, which are displayed in the lower right corner.

(It needs to run on a real machine. At least, on the simulator, I don't know how to simulate the screenshot (Home + Power). If you know, I 'd like to tell you)


Source code git download link: colin1994/TakeScreenshotTest


I. Registration notification:

// Register the notification [[NSNotificationCenter defacenter center] addObserver: self selector: @ selector (userDidTakeScreenshot :) name: UIApplicationUserDidTakeScreenshotNotification object: nil];


II. Listen to screenshots:

Execute the operation, that is, implement the response function corresponding to the above notification-userDidTakeScreenshot

// Screenshot response-(void) userDidTakeScreenshot :( NSNotification *) notification {NSLog (@ ""); // a human screenshot is taken to simulate user screenshots, obtain the UIImage * image _ = [self imageWithScreenshot]; // Add the UIImageView * imgvPhoto = [[UIImageView alloc] initWithImage: image _]; imgvPhoto. frame = CGRectMake (self. window. frame. size. width/2, self. window. frame. size. height/2, self. window. frame. size. width/2, self. window. frame. size. height/2); // Add the border CALayer * layer = [imgvPhoto layer]; layer. borderColor = [[UIColor whiteColor] CGColor]; layer. borderWidth = 5.0f; // Add four side shadows imgvPhoto. layer. shadowColor = [UIColor blackColor]. CGColor; imgvPhoto. layer. shadowOffset = CGSizeMake (0, 0); imgvPhoto. layer. shadowOpacity = 0.5; imgvPhoto. layer. shadowRadius = 10.0; // Add two side shadows imgvPhoto. layer. shadowColor = [UIColor blackColor]. CGColor; imgvPhoto. layer. shadowOffset = CGSizeMake (4, 4); imgvPhoto. layer. shadowOpacity = 0.5; imgvPhoto. layer. shadowRadius = 2.0; [self. window addSubview: imgvPhoto];}


My userDidTakeScreenshot has done three tasks in total.

1. Print the screenshot Detected

2. Obtain screenshot images. Call [self imageWithScreenshot]. Here, imageWithScreenshot is a human screenshot, simulating user screenshot operations and obtaining screenshot images.

3. The screenshot is displayed in the lower right corner of the screen, 1/4 in size, and highlighted with a white border and shadow effect.


3. Obtain screenshot Images

/*** Capture the current screen ** @ return NSData **/-(NSData *) dataWithScreenshotInPNGFormat {CGSize imageSize = CGSizeZero; UIInterfaceOrientation orientation = [UIApplication sharedApplication]. statusBarOrientation; if (UIInterfaceOrientationIsPortrait (orientation) imageSize = [UIScreen mainScreen]. bounds. size; else imageSize = CGSizeMake ([UIScreen mainScreen]. bounds. size. height, [UIScreen mainScreen]. bounds. size. width); histogram (imageSize, NO, 0); CGContextRef context = UIGraphicsGetCurrentContext (); for (UIWindow * window in [[UIApplication sharedApplication] windows]) {CGContextSaveGState (context ); CGContextTranslateCTM (context, window. center. x, window. center. y); CGContextConcatCTM (context, window. transform); CGContextTranslateCTM (context,-window. bounds. size. width * window. layer. anchorPoint. x,-window. bounds. size. height * window. layer. anchorPoint. y); if (orientation = UIInterfaceOrientationLandscapeLeft) {CGContextRotateCTM (context, M_PI_2); CGContextTranslateCTM (context, 0,-imageSize. width);} else if (orientation = UIInterfaceOrientationLandscapeRight) {CGContextRotateCTM (context,-M_PI_2); CGContextTranslateCTM (context,-imageSize. height, 0);} else if (orientation = UIInterfaceOrientationPortraitUpsideDown) {CGContextRotateCTM (context, M_PI); CGContextTranslateCTM (context,-imageSize. width,-imageSize. height);} if ([window respondsToSelector: @ selector (drawViewHierarchyInRect: afterScreenUpdates :)]) {[window drawViewHierarchyInRect: window. bounds afterScreenUpdates: YES];} else {[window. layer renderInContext: context];} CGContextRestoreGState (context);} UIImage * image = Response (); UIGraphicsEndImageContext (); return UIImagePNGRepresentation (image );} /*** return the captured image ** @ return UIImage **/-(UIImage *) imageWithScreenshot {NSData * imageData = [self dataWithScreenshotInPNGFormat]; return [UIImage imageWithData: imageData];}




How to display screenshots

To intercept the screen, use the screenshot key print next to F12, and then use a drawing tool (windows system itself has such a "Drawing" function, you can find it at the start, program, attachment, and drawing. paste and save it, but save it in BMP format. Software with PS software can be converted to other formats, such as GIF, JPG.
If you only want to capture the image of the current window, you can press ALT and press print to keep up with it.
Now there is A very practical function on QQ, ALT + CTRL + A, you can cut down the screen range and size you want to cut. if you have QQ, you can try it. It is also a very convenient tool. You can add the PS software to change it to your favorite image.

Press the screenshot key on the keyboard. In which folder is the captured image?

It is not automatically saved. You need to save it yourself. You can put it in Word documents, drawing, QQ, etc.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.