IOS screenshot Single example code

Source: Internet
Author: User

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];




  

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.