ios摇一摇截屏代码

#import "ViewController.h"

 

@interface ViewController ()

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    //1.添加一个视图

    UIView *greenView=[[UIView alloc]init];

    greenView.frame=CGRectMake(100, 100, 100, 100);

    greenView.backgroundColor=[UIColor greenColor];

    [self.view addSubview:greenView];

    //设置第一响应事件(必须做的)

    [self becomeFirstResponder];

    

    

}

-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event

{

    if (motion==UIEventSubtypeMotionShake) {

        //截屏

        [self snapshot];

    }

}

 

//截屏

-(void)snapshot

{

    //开启上下文

    UIGraphicsBeginImageContext(self.view.bounds.size);

    //拿到上下文

    CGContextRef context=UIGraphicsGetCurrentContext();

    [self.view.layer renderInContext:context];

    UIImage *image=UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    //保存到相册

    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo

{

    if (!error) {

        NSLog(@"save success");

    }

    

}

 

@end