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
@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
相关文章推荐
- 我的BIOS之行(3)-遍历pci设备(1)io访问
- 去除iOS 6程序图标的玻璃质感效果
- 在ios中举个简单的protocol例子,关于两个类用协议方式传值。
- iOS 重写 isequal方法需重写hash方法
- iOS中nil 、NULL、 Nil 、NSNull的区别详谈
- 【我们都爱Paul Hegarty】斯坦福IOS8公开课个人笔记37 TableView Delegate
- IOS应用
- 一个猜数字输赢的小游戏
- iOS初学者第四天
- iOS 字符串读写
- iOS 即使通讯第三方SDK 资料
- 《慕客网:IOS基础入门之Foundation框架初体验》学习笔记 <一>
- ios 距离传感器和摇一摇
- wxhl ios bj 49 张浩 学习心得连载 第一章
- iostream.h和iostream 区别,“cout”: 未声明的标识符”
- iOS开发中,关于什么时候使用点语法的解答
- iOS Touch ID使用
- iOS常用设计模式之观察者模式
- 一个仿IOS风格的AlertDialog
- iOS常用设计模式之委托模式