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.
- UIView * view = [[[[UIApplication sharedApplication] windows] objectAtIndex: 1] subviews] lastObject]; // obtain a subView of a window
- NSInteger index = 0; // used to name the saved png
- For (UIView * subView in [view subviews]) {// traverses the subViews of this view
- If ([subView isKindOfClass: NSClassFromString (@ "UIImageView")] | [subView isKindOfClass: NSClassFromString (@ "UIThreePartButton")])
- {// Find the desired subView
- // The key to supporting high retina scores
- If (uigraphicsbeginimagecontextwitexceptions! = NULL)
- {
- Uigraphicsbeginimagecontextwittions (subView. frame. size, NO, 0.0 );
- } Else {
- UIGraphicsBeginImageContext (subView. frame. size );
- }
- // Obtain the image
- [SubView. layer renderInContext: UIGraphicsGetCurrentContext ()];
- UIImage * image = UIGraphicsGetImageFromCurrentImageContext ();
- UIGraphicsEndImageContext ();
- // Save the image
- NSString * path = [NSHomeDirectory () stringByAppendingFormat: @ "/mongod.png", index];
- If ([UIImagePNGRepresentation (image) writeToFile: path atomically: YES]) {
- Index + = 1;
- NSLog (@ "Succeeded! ");
- }
- Else {
- NSLog (@ "Failed! ");
- }
- }
- }
Summary:IPhone DevelopmentApplication code implementationScreenshotsI hope this article will be helpful to you.