转载自:http://m.open-open.com/m/code/view/1420469506375

1.普通界面

    /**
*截图功能
*/
-(void)screenShot{
UIGraphicsBeginImageContextWithOptions(CGSizeMake(640, 960), YES, 0); //设置截屏大小 [[self.view layer] renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); CGImageRef imageRef = viewImage.CGImage;
CGRect rect = CGRectMake(0, 0, 641, SCREEN_HEIGHT + 300);//这里可以设置想要截图的区域 CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, rect);
UIImage *sendImage = [[UIImage alloc] initWithCGImage:imageRefRect]; //以下为图片保存代码 UIImageWriteToSavedPhotosAlbum(sendImage, nil, nil, nil);//保存图片到照片库 NSData *imageViewData = UIImagePNGRepresentation(sendImage);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pictureName= @"screenShow.png";
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:pictureName];
[imageViewData writeToFile:savedImagePath atomically:YES];//保存照片到沙盒目录 CGImageRelease(imageRefRect); //从手机本地加载图片 UIImage *bgImage2 = [[UIImage alloc]initWithContentsOfFile:savedImagePath]; }

2.UIScrollView截屏(一屏无法显示完整)

    /**
*截图
*/
- (void)screenShot{
UIImage* image = nil;
UIGraphicsBeginImageContext(m_scrollView.contentSize); {
CGPoint savedContentOffset = m_scrollView.contentOffset;
CGRect savedFrame = m_scrollView.frame;
m_scrollView.contentOffset = CGPointZero; m_scrollView.frame = CGRectMake(0, 0, m_scrollView.contentSize.width, m_scrollView.contentSize.height); [m_scrollView.layer renderInContext: UIGraphicsGetCurrentContext()]; image = UIGraphicsGetImageFromCurrentImageContext(); m_scrollView.contentOffset = savedContentOffset;
m_scrollView.frame = savedFrame;
}
UIGraphicsEndImageContext(); if (image != nil) {
NSLog(@"截图成功!"); }
}
截全屏的代码:
- (UIImage*)screenView:(UIView *)view{
CGRect rect = view.frame;
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[view.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
如果需要导航栏也截取的话
把[view.layer renderInContext:context]改成

[self.navigationController.view.layer renderInContext:context]
 

iOS截屏代码的更多相关文章

  1. ios截屏代码[转]

    http://www.cnblogs.com/chenxiangxi/p/3547974.html 这位博主的连接中将ios自定义大小位置的截屏代码写的很不错,马上就能用的方法,对于只想马上用的程序员 ...

  2. ios摇一摇截屏代码

    #import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...

  3. iOS 截屏分享(包含状态栏与不包含状态栏)

    iOS8以上的新方法PhotoKit 监听截图相册变化,取最后一张图片:http://www.hangge.com/blog/cache/detail_1515.html PhotoKit 获取本机相 ...

  4. iOS截屏功能

    代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // ...

  5. 【转】Android 音量键+电源键 截屏代码小结

    http://104zz.iteye.com/blog/1752961 原文地址:http://blog.csdn.net/hk_256/article/details/7306590 ,转载请注明出 ...

  6. iOS截屏保存至相册

    #pragma mark 截屏并保存至相册 -(void)screenShotsComplete:(void(^)(UIImage * img)) complete { CGSize imageSiz ...

  7. iOS截屏并修改截图然后分享的功能实现

    一. 实现的效果类似微博的截图分享 不仅截图分享的时候还进行图片的修改,增加自己的二维码 二.实现方式 苹果在ios7之后提供了一个新的通知类型:UIApplicationUserDidTakeScr ...

  8. iOS截屏方法

    //获取屏幕截屏方法 - (UIImage *)capture { // 创建一个context UIGraphicsBeginImageContextWithOptions(self.view.bo ...

  9. IOS 截屏(保存到相册中)

    @interface NJViewController () /** * 点击截屏按钮 */ - (IBAction)captureView:(UIButton *)sender; /** * 白色v ...

随机推荐

  1. NHibernate加载DLL错误

    这几天在开发关于Rest的服务,其中用到了NHibernate来进行数据库交互,突然有一天发现了一个错误,如下: Could not load file or assembly 'NHibernate ...

  2. jQuery获取属性之自己遇到的问题

    刚开始是这种写法  用的 attr  结果获取不到 if($("#reg_username_span").attr("display") == 'block') ...

  3. HDOJ 1004题 Let the Balloon Rise strcmp()函数

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

  4. poj 3304 Segments(计算几何基础)

      Segments Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11593   Accepted: 3657 Descr ...

  5. Bzoj 2120: 数颜色 && 2453: 维护队列 莫队,分块,bitset

    2120: 数颜色 Time Limit: 6 Sec  Memory Limit: 259 MBSubmit: 2645  Solved: 1039[Submit][Status][Discuss] ...

  6. Closure Compiler(封闭编辑器), Closure Inspector, Closure Templates, 封闭图书馆(Closure Library) Google- 摘自网络

    谷歌日前宣布,将自己开发者使用的一系列工具对外开放.这些工具曾用来开发谷歌的主要产品,包括Gmail.谷歌文档(Google Docs)和谷歌地图(Google Maps). 第一个工具叫做Closu ...

  7. html自定义checkbox、radio、select —— checkbox、radio篇

    前些日子,所在公司项目的UI做了大改,前端全部改用 Bootstrap 框架,Bootstrap的优缺点在此就不详述了,网上一大堆相关资料. 前端的设计就交给我和另一个同事[LV,大学同班同学,毕业后 ...

  8. Robot Framework自动化测试环境准备(一)

    Robot framework是诺西(NSN)开源的一套自动化测试工具,在通信设备自动化测试中很实用,它基于Python开发,主要模拟NMS网管配置数据到网元NODE,并读取配置看配置是否生效. == ...

  9. WebBindingInitializer学习

    今天在看开源代码的时候遇到了这个接口,于是查阅了下相关的资料 Spring web mvc学习笔记 http://www.cnblogs.com/crazy-fox/archive/2012/02/1 ...

  10. linux ssh scp无密码登录

    一. 应用场景 假如你Linux Client是客户端, Server为服务器,用户名为user.现在要配置从Client到Server的无密码SSH登录或者无密码的scp拷贝. 例如客户端Clien ...