iOS 学习笔记一【屏幕截图,并显示当前View】
2015-11-12 19:03
411 查看
// 直接上代码:
// // ViewController.h // 屏幕截图测试 // // Created by 博爱之家 on 15/11/11. // Copyright © 2015年 博爱之家. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController { NSData *imageData; } @end
*************************************
// // ViewController.m // 屏幕截图测试 // // Created by 博爱之家 on 15/11/11. // Copyright © 2015年 博爱之家. All rights reserved. // #import "ViewController.h" //宏定义 //当前设备的屏幕宽度 #define KSCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width //当前设备的屏幕高度 #define KSCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height @interface ViewController () @property (nonatomic, strong) UILabel *testlabel; @property (nonatomic, strong) UIButton *testButton; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.title = @"截屏测试"; self.view.backgroundColor = [UIColor whiteColor]; self.testlabel = [UILabel new]; self.testlabel.frame = CGRectMake((KSCREEN_WIDTH-200)/2 , 100, 200, 50); self.testlabel.text = @"截屏测试"; self.testButton = [UIButton buttonWithType:UIButtonTypeCustom]; self.testButton.frame = CGRectMake((KSCREEN_WIDTH-200)/2 , 200, 200, 50); [self.testButton setTitle:@"点击截屏" forState:UIControlStateNormal]; self.testButton.backgroundColor = [UIColor purpleColor]; [self.testButton addTarget:self action:@selector(clickBUutton:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.testlabel]; [self.view addSubview:self.testButton]; } - (IBAction)clickBUutton:(id)sender { NSLog(@"开始"); [self saveScreenShotsView]; UIImageView *imageView = [[UIImageView alloc] init]; imageView.frame = CGRectMake(100, 400, 200, 200); imageView.backgroundColor = [UIColor greenColor]; imageView.image = [UIImage imageWithData:imageData]; [self.view addSubview:imageView]; } // 保存图片 - (void)saveScreenShotsView { UIImage *image = [self getNormalImage:self.view]; UIImageWriteToSavedPhotosAlbum(image, self, nil, nil); [self saveToDisk:image]; NSLog(@"结束"); } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } #pragma mark - 获取屏幕截图 - (UIImage *)getNormalImage:(UIView *)view { UIGraphicsBeginImageContext(CGSizeMake(KSCREEN_WIDTH, KSCREEN_HEIGHT)); CGContextRef context = UIGraphicsGetCurrentContext(); [view.layer renderInContext:context]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } #pragma mark - 保存到硬盘中 - (void)saveToDisk:(UIImage *)image { NSString *dirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSLog(@"保存路径: %@", dirPath); NSString *path = [NSString stringWithFormat:@"%@/pic_%f.png",dirPath,[NSDate timeIntervalSinceReferenceDate]]; imageData = [NSData dataWithData:UIImagePNGRepresentation(image)]; [imageData writeToFile:path atomically:YES]; NSLog(@"保存路径: %@", path); NSString *imagePath = [[path componentsSeparatedByString:@"/"] lastObject]; NSLog(@"保存路径2imagePath: %@", imagePath); NSLog(@"保存完毕"); } @end
相关文章推荐
- iOS开发之MAC常用快捷键汇总
- iOS开发零基础教程之真机调试流程
- iOS开发零基础教程之开发证书以及打包问题
- iOS9 警告框
- ios中青花瓷抓包过程
- Understanding iOS Exception Types
- iOS 定位服务、通讯录、日历、提醒事项、照片、蓝牙共享、麦克风、相机等授权检测
- iOS开发 -- Xcode7如何创建项目启动图!?
- iOS 支持webrtc的浏览器 bowser
- 查看iOS应用的推广渠道
- iOS开发 -- 添加自定义ttf字体的方法
- iOS常见错误3-Xcode导入第三方库 'MAMapKit/MAMapKit.h' file not found('XXXX/XXXX.h' file not found)错误
- iOS小技巧11-Xcode中相对路径和绝对路径的使用
- 使用Xcode 7 免费真机调试iOS应用程序
- IOS中KVO模式的解析与应用
- iOS 保持界面流畅的技巧
- iOS 保持界面流畅的技巧
- iOS开发之集成支付宝
- IOS 富文本---关于如何处理在一段文字中突出显示某几个字段大小或者颜色的处理
- IOS解析JSON数据格式错误一