one. The full screen code for IOS is:
UIWindow *screenwindow = [[UIApplication sharedapplication] Keywindow]; 1. Screenshot Uigraphicsbeginimagecontext (screenWindow.frame.size); [Screenwindow.layer Renderincontext:uigraphicsgetcurrentcontext ()]; UIImage *screenshot = Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext ();
two. Intercept the specified area:
uigraphicsbeginimagecontextwithoptions( Self. View.bounds.size, Self. View.opaque,0.0);
[ Self. Myview.layer Renderincontext:Uigraphicsgetcurrentcontext()];
UIImage*theimage=Uigraphicsgetimagefromcurrentimagecontext();
Uigraphicsendimagecontext();
NSData*theimagedata=uiimagejpegrepresentation(Theimage,1.0 );//you can use PNG too
[Theimagedata writetofile:@"Example.jpeg"Atomically:yes];
three. Single example of screenshot code
ScreenShot.h File
#import <Foundation/Foundation.h> @interface screenshot:nsobject///screenshot picture location @property (nonatomic,copy) NSString *filepath;/* Single-instance function */+ (ID) sharedscreenshot;/* screenshot, and write memory */-(void) getscreenimage; @end
screenshot.m File
#import "ScreenShot.h"//Screenshot Picture # Screenshotimage @ "Screenshot.png" @implementation screenshot@synthesize filepath=_filepath;-(ID) init {if (self = [super init]) {_filepath=[self Documentspathforfilename:screenshotim Age]; } return self;} #pragma mark-Single case + (ID) sharedscreenshot {static screenshot *screenshot = nil; Static dispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{screenshot = [[Self alloc] init]; }); return screenshot;} #pragma mark gets the path by file name-(NSString *) Documentspathforfilename: (NSString *) name{nsarray *paths = Nssearchpathfordirector Iesindomains (Nsdocumentdirectory,nsuserdomainmask, YES); NSString *documentspath = [Paths objectatindex:0]; return [Documentspath stringbyappendingpathcomponent:name];} #pragma mark-screenshot-(void) getscreenimage{UIWindow *screenwindow = [[UIApplication sharedapplication] Keywindow]; 1. Screenshot Uigraphicsbeginimagecontext (screenWindow.frame.size); [Screenwindow.layer RenderincontexT:uigraphicsgetcurrentcontext ()]; UIImage *screenshot = Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext (); 2. Save NSData *screenshotpng = uiimagepngrepresentation (screenshot); Nserror *error = nil; 3. Write memory [screenshotpng writetofile:_filepath options:nsatomicwrite error:&error];} @end
four. Use:
1. Introduce the header file in the Viewcontroller ScreenShot.h
//screenshot and save [screenshot sharedscreenshot] getscreenimage];
2. Get Pictures
NSString *shotfilepath= ((screenshot *) [screenshot sharedscreenshot]). FilePath; NSData *shotimagedata = [NSData Datawithcontentsoffile:shotfilepath]; UIImage *shotimage = [UIImage imagewithdata:shotimagedata];