您的位置:首页 > 移动开发 > IOS开发

ios摇一摇截屏代码

2015-07-19 23:58 639 查看
#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: