Develop app code for iPhone to implement screenshot Function

Source: Internet
Author: User

IPhone DevelopmentThe screenshot function of application code is described in this article. The content is mainly explained by code. Let's look at code implementation first.

IPhoneYou can press the Home and lock screen keys at the same time.ScreenshotsBut in the application, you cannot jump out of a line to let the user cutScreenKey. The Code shared by the CocoaChina member "bobgreen" can implement the screenshot function in the program and support the iPhone 4 Retina high-split screen.

 
 
  1. UIView * view = [[[[UIApplication sharedApplication] windows] objectAtIndex: 1] subviews] lastObject]; // obtain a subView of a window
  2. NSInteger index = 0; // used to name the saved png
  3. For (UIView * subView in [view subviews]) {// traverses the subViews of this view
  4. If ([subView isKindOfClass: NSClassFromString (@ "UIImageView")] | [subView isKindOfClass: NSClassFromString (@ "UIThreePartButton")])
  5. {// Find the desired subView
  6. // The key to supporting high retina scores
  7. If (uigraphicsbeginimagecontextwitexceptions! = NULL)
  8. {
  9. Uigraphicsbeginimagecontextwittions (subView. frame. size, NO, 0.0 );
  10. } Else {
  11. UIGraphicsBeginImageContext (subView. frame. size );
  12. }
  13. // Obtain the image
  14. [SubView. layer renderInContext: UIGraphicsGetCurrentContext ()];
  15. UIImage * image = UIGraphicsGetImageFromCurrentImageContext ();
  16. UIGraphicsEndImageContext ();
  17. // Save the image
  18. NSString * path = [NSHomeDirectory () stringByAppendingFormat: @ "/mongod.png", index];
  19. If ([UIImagePNGRepresentation (image) writeToFile: path atomically: YES]) {
  20. Index + = 1;
  21. NSLog (@ "Succeeded! ");
  22. }
  23. Else {
  24. NSLog (@ "Failed! ");
  25. }
  26. }
  27. }

Summary:IPhone DevelopmentApplication code implementationScreenshotsI hope this article will be helpful to you.

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.