码迷,mamicode.com
首页 > 移动开发 > 详细

ios摇一摇截屏代码

时间:2015-07-20 01:04:17      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

#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

ios摇一摇截屏代码

标签:

原文地址:http://www.cnblogs.com/tangranyang/p/4660233.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!