#import "ViewController.h"
@interface viewcontroller ()
@end
@implementation Viewcontroller
-(void) viewdidload {
[Super viewdidload];
//1. Add a View
UIView *greenview=[[UIView alloc]init];
Greenview. Frame= CGRectMake (+, +, + );
Greenview. backgroundcolor=[uicolor greencolor];
[self. View addsubview: Greenview];
// Set First Response event ( required )
[self becomefirstresponder];
}
-(void) Motionbegan: (uieventsubtype) motion withevent: (uievent *) event
{
if (motion==uieventsubtypemotionshake) {
// screenshot
[self snapshot];
}
}
Screen Cutting
-(void) Snapshot
{
// Open Context
Uigraphicsbeginimagecontext(self. View. Bounds. Size);
// Get context
cgcontextref context=uigraphicsgetcurrentcontext();
[self. View. Layer Renderincontext: Context];
UIImage *image=uigraphicsgetimagefromcurrentimagecontext();
Uigraphicsendimagecontext();
// save to album
uiimagewritetosavedphotosalbum(image, self, @selector(image:didfinishsavingwitherror: ContextInfo:), nil);
}
-(void) Image: (UIImage *) image didfinishsavingwitherror: (nserror *) Error ContextInfo :(void *) contextinfo
{
if (!error) {
NSLog(@ "save Success");
}
}
@end
iOS shaking a screenshot of the screen code