// 直接上代码:

//

//  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-)/ , , , );

    self.testlabel.text = @"截屏测试";

    self.testButton = [UIButton buttonWithType:UIButtonTypeCustom];

    self.testButton.frame = CGRectMake((KSCREEN_WIDTH-)/ , , , );

    [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(, , , );

    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:];

    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 学习笔记一【屏幕截图,并显示当前View】的更多相关文章

  1. iOS学习笔记(5)——显示简单的TableView

    1. 创建工程 创建一个新的Xcode工程命名为SimpleTableTest. 删除main.storyboard文件和info.plist中有关storyboard的相关属性. 按command+ ...

  2. iOS学习笔记(4)——显示单组件选取器

    1. 创建工程 创建新工程,create a new Xcode project 创建single view application 创建名为PickerViewTest的工程 2. 创建xib文件 ...

  3. iOS学习笔记-精华整理

    iOS学习笔记总结整理 一.内存管理情况 1- autorelease,当用户的代码在持续运行时,自动释放池是不会被销毁的,这段时间内用户可以安全地使用自动释放的对象.当用户的代码运行告一段 落,开始 ...

  4. iOS学习笔记10-UIView动画

    上次学习了iOS学习笔记09-核心动画CoreAnimation,这次继续学习动画,上次使用的CoreAnimation很多人感觉使用起来很繁琐,有没有更加方便的动画效果实现呢?答案是有的,那就是UI ...

  5. iOS学习笔记总结整理

    来源:http://mobile.51cto.com/iphone-386851_all.htm 学习IOS开发这对于一个初学者来说,是一件非常挠头的事情.其实学习IOS开发无外乎平时的积累与总结.下 ...

  6. IOS学习笔记(四)之UITextField和UITextView控件学习

    IOS学习笔记(四)之UITextField和UITextView控件学习(博客地址:http://blog.csdn.net/developer_jiangqq) Author:hmjiangqq ...

  7. [置顶] iOS学习笔记47——图片异步加载之EGOImageLoading

    上次在<iOS学习笔记46——图片异步加载之SDWebImage>中介绍过一个开源的图片异步加载库,今天来介绍另外一个功能类似的EGOImageLoading,看名字知道,之前的一篇学习笔 ...

  8. iOS学习笔记之UITableViewController&UITableView

    iOS学习笔记之UITableViewController&UITableView 写在前面 上个月末到现在一直都在忙实验室的事情,与导师讨论之后,发现目前在实验室完成的工作还不足以写成毕业论 ...

  9. IOS学习笔记48--一些常见的IOS知识点+面试题

      IOS学习笔记48--一些常见的IOS知识点+面试题   1.堆和栈什么区别? 答:管理方式:对于栈来讲,是由编译器自动管理,无需我们手工控制:对于堆来说,释放工作由程序员控制,容易产生memor ...

  10. iOS学习笔记-自己动手写RESideMenu

    代码地址如下:http://www.demodashi.com/demo/11683.html 很多app都实现了类似RESideMenu的效果,RESideMenu是Github上面一个stars数 ...

随机推荐

  1. [Eclipse]--Error:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path.

    一段时间没用eclipse后,再去打开以前的项目,发现一打开前线标红.查看错误的时候,如下图所示: Error:The superclass "javax.servlet.http.Http ...

  2. Target runtime Apache Tomcat 7.1 is not defined 解决方法

    在工作台目录下找到 自己操作的项目的文件夹 /.settings/org.eclipse.wst.common.project.facet.core.xml 打开后,显示 <?xml versi ...

  3. Problem L: 输出200-299之间的所有素数

    #include<stdio.h> #include<math.h> int main() { int count,m,n,i; count=; ;m<;m++) { n ...

  4. INLINE-BLOCK和FLOAT(二)(转)

    一.一抹前言 没有爱的日子,时间如指尖细沙,不知不觉就流逝了.写“CSS float浮动的深入研究.详解及拓展(一)”和“CSS float浮动的深入研究.详解及拓展(二)”似乎就在不久前,然而相隔差 ...

  5. Android中选项卡功能的实现

    Android中选项卡功能的实现 Android中使用TabHost和TabWidget来实现选项卡功能.TabHost必须是布局的根节点,它包含两个子节点: TabWidget,显示选项卡: Fra ...

  6. RxJava 2.x 理解-3

    背压:Flowable / Subscriber 在RxJava 1.x 理解 中,没有讲到背压这个概念,是因为学习太落后了,RxJava都出2了,所以直接在2上学. 背压是下游控制上游流速的一种手段 ...

  7. java通过Comparable接口实现字符串比较大小排序的简单实例

    /** * 对象比较大小compare的用法 字符串排序 * 练习代码, 给定字符串" nba" "cba" "ncaa" "wb ...

  8. centos6.8下安装部署LNMP(备注:nginx1.8.0+php5.6.10+mysql5.6.12)

    在平时运维工作中,经常需要用到LNMP应用框架.以下对LNMP环境部署记录下: 1)前期准备:为了安装顺利,建议先使用yum安装依赖库[root@opd ~]#yum install -y make ...

  9. VR/AR工作原理、目前存在的技术问题

    http://blog.csdn.net/liulong1567/article/details/50686558 摘要: 这些挑战,每一个都还需要很多努力才能解决.目前它们很多都还不到量变(只是需要 ...

  10. Python \xd7\xaa\xd5\xbdOTT TV\xb1\xa6\xbd\xe0 编码

    import chardet s = '\xd7\xaa\xd5\xbdOTT TV\xb1\xa6\xbd\xe0\xc7\xa3\xca\xd6\xd2\xf8\xba\xd3\xa1\xa4\x ...