IOS截屏的方法网上有很多,以下是我个人认为比较好的一个,我稍微改了一点

来源:SDScreenshotCapture

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
UIImage *getImageWithFullScreenshot(void)
{// Source (Under MIT License): https://github.com/shinydevelopment/SDScreenshotCapture/blob/master/SDScreenshotCapture/SDScreenshotCapture.m#L35BOOL ignoreOrientation = SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0");UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;CGSize imageSize = CGSizeZero;if (UIInterfaceOrientationIsPortrait(orientation) || ignoreOrientation)imageSize = [UIScreen mainScreen].bounds.size;elseimageSize = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);UIGraphicsBeginImageContextWithOptions(imageSize, NO, [UIScreen mainScreen].scale);CGContextRef context = UIGraphicsGetCurrentContext();for (UIWindow *window in [[UIApplication sharedApplication] windows]){CGContextSaveGState(context);CGContextTranslateCTM(context, window.center.x, window.center.y);CGContextConcatCTM(context, window.transform);CGContextTranslateCTM(context, -window.bounds.size.width * window.layer.anchorPoint.x, -window.bounds.size.height * window.layer.anchorPoint.y);// Correct for the screen orientationif(!ignoreOrientation){if(orientation == UIInterfaceOrientationLandscapeLeft){CGContextRotateCTM(context, (CGFloat)M_PI_2);CGContextTranslateCTM(context, 0, -imageSize.width);}else if(orientation == UIInterfaceOrientationLandscapeRight){CGContextRotateCTM(context, (CGFloat)-M_PI_2);CGContextTranslateCTM(context, -imageSize.height, 0);}else if(orientation == UIInterfaceOrientationPortraitUpsideDown){CGContextRotateCTM(context, (CGFloat)M_PI);CGContextTranslateCTM(context, -imageSize.width, -imageSize.height);}}if([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])[window drawViewHierarchyInRect:window.bounds afterScreenUpdates:NO];else[window.layer renderInContext:UIGraphicsGetCurrentContext()];CGContextRestoreGState(context);}UIImage *image = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();return image;
}

以上是全屏截图,下面修改部分区域截图,以下代码是UIWindow的Category

h文件

#import <UIKit/UIKit.h>@interface UIWindow (Category)- (UIImage *)screenshot;
- (UIImage *)screenshotWithRect:(CGRect)rect;@end

m文件

#import "UIWindow+Category.h"@implementation UIWindow (Category)- (UIImage *)screenshot
{return [self screenshotWithRect:self.bounds];
}- (UIImage *)screenshotWithRect:(CGRect)rect
{// Source (Under MIT License): https://github.com/shinydevelopment/SDScreenshotCapture/blob/master/SDScreenshotCapture/SDScreenshotCapture.m#L35BOOL ignoreOrientation = SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0");UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;CGSize imageSize = CGSizeZero;CGFloat width = rect.size.width, height = rect.size.height;CGFloat x = rect.origin.x, y = rect.origin.y;//    imageSize = CGSizeMake(width, height);
//    UIGraphicsBeginImageContextWithOptions(imageSize, NO, [UIScreen mainScreen].scale);if (UIInterfaceOrientationIsPortrait(orientation) || ignoreOrientation){//imageSize = [UIScreen mainScreen].bounds.size;imageSize = CGSizeMake(width, height);x = rect.origin.x, y = rect.origin.y;}else{//imageSize = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);imageSize = CGSizeMake(height, width);x = rect.origin.y, y = rect.origin.x;}UIGraphicsBeginImageContextWithOptions(imageSize, NO, [UIScreen mainScreen].scale);CGContextRef context = UIGraphicsGetCurrentContext();CGContextSaveGState(context);CGContextTranslateCTM(context, self.center.x, self.center.y);CGContextConcatCTM(context, self.transform);CGContextTranslateCTM(context, -self.bounds.size.width * self.layer.anchorPoint.x, -self.bounds.size.height * self.layer.anchorPoint.y);// Correct for the screen orientationif(!ignoreOrientation){if(orientation == UIInterfaceOrientationLandscapeLeft){CGContextRotateCTM(context, (CGFloat)M_PI_2);CGContextTranslateCTM(context, 0, -self.bounds.size.height);CGContextTranslateCTM(context, -x, y);}else if(orientation == UIInterfaceOrientationLandscapeRight){CGContextRotateCTM(context, (CGFloat)-M_PI_2);CGContextTranslateCTM(context, -self.bounds.size.width, 0);CGContextTranslateCTM(context, x, -y);}else if(orientation == UIInterfaceOrientationPortraitUpsideDown){CGContextRotateCTM(context, (CGFloat)M_PI);CGContextTranslateCTM(context, -self.bounds.size.height, -self.bounds.size.width);CGContextTranslateCTM(context, x, y);}else{CGContextTranslateCTM(context, -x, -y);}}else{CGContextTranslateCTM(context, -x, -y);}//[self layoutIfNeeded];if([self respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:NO];else[self.layer renderInContext:UIGraphicsGetCurrentContext()];CGContextRestoreGState(context);UIImage *image = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();return image;
}@end

此代码在旋转后,裁剪区域是相对左上角为原点旋转的,一般使用不到旋转情况

View截图

h文件

@interface UIView (Screenshot)
- (UIImage *)screenshot;
- (UIImage *)screenshotWithRect:(CGRect)rect;
@end

m文件

@implementation UIView (Screenshot)- (UIImage *)screenshot
{return [self screenshotWithRect:self.bounds];
}- (UIImage *)screenshotWithRect:(CGRect)rect;
{UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);CGContextRef context = UIGraphicsGetCurrentContext();if (context == NULL){return nil;}CGContextSaveGState(context);CGContextTranslateCTM(context, -rect.origin.x, -rect.origin.y);//[self layoutIfNeeded];if( [self respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]){[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:NO];}else{[self.layer renderInContext:context];}CGContextRestoreGState(context);UIImage *image = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();//    NSData *imageData = UIImageJPEGRepresentation(image, 1); // convert to jpeg
//    image = [UIImage imageWithData:imageData scale:[UIScreen mainScreen].scale];return image;
}

IOS截屏,View截图的基本方法相关推荐

  1. ios截屏功能html,滚动截屏APP - iPhone上的长截图工具

    话说长截图功能也算是一种刚需了,如今安卓好多手机系统都会自带此功能.很难想象的是,安卓手机标配的「长截图」功能,对果粉来说是多么的奢侈.iPhone没有自带的长截图功能,只能借助第三方APP,比如Ta ...

  2. 怎么用计算机截图快捷键,电脑怎么截图截屏 电脑截图的快捷键是什么

    电脑怎么截图截屏 电脑截图的快捷键是什么 来源:www.18183.com作者:皮卡时间:2015-10-23 很多人打开电脑的时候就会习惯性的登上各类社交应用,例如QQ.阿里旺旺.微博客户端等,这些 ...

  3. 计算机基础知识截屏,电脑截屏技巧 截图快捷键是什么

    电脑截屏技巧 截图快捷键是什么 在我们日常使用电脑不管是工作还是娱乐都经常会使用到截图功能,下面我们来介绍使用截图快捷键来实现快速截图的方法. 目前我们比较常用的有3种截图快捷键,分别是全屏截图.当前 ...

  4. 电脑计算机科学模式截屏,电脑截图的几种方法,简单实用-电脑怎么截图

    经常使用电脑的小伙伴,经常会需要用到截图功能,可能是一部分区域,也可能是整个屏幕.用手机拍的话会很麻烦,而且拍出来的照片也不够清晰.那么都有哪些简单实用的截图方法呢?小Y来告诉你~ 1.键盘上的快捷键 ...

  5. iOS截屏功能的实现

    使用场景: 本人在自定义UITabBarController的时候,想要实现手势滑动返回上一页面的效果,也就是仿QQ的滑动返回,这种滑动返回是基于UIPanGestureRecognizer这一手势, ...

  6. iOS 截屏的总结,AVplayer的截屏,当前屏幕的截屏,截屏后导航栏不见的情况

    转载自: http://www.jianshu.com/p/8e9234a70955 1.截取当前屏幕,导航栏不见. 方法: - (UIImage*)screenView:(UIView *)view ...

  7. iOS截屏“闪现效果实现”

    iOS系统截屏,按住'home'键和'电源'键,屏幕会闪烁一下生成一张截图,保存至相册. 简单实现: 1-> UIButton UILabel 2-> button注册点击事件 3-> ...

  8. ios截屏 u3d导出Xcode工程截屏

    在ios原生程序里面 全屏截屏 UIWindow *window = [[UIApplication sharedApplication] keyWindow]; //下边是经典的4步 UIGraph ...

  9. 快捷键截屏_win7截图快捷键是什么 win7截图快捷键怎么按

    win7自带截图快捷键,Win7屏幕截图的快捷键是固定的,而Win7自动截图工具的快捷键就可以自己设置,下面就给大家分享一下win7截图快捷键是什么. 品牌型号:联想GeekPro 2020 系统:w ...

最新文章

  1. 【linux】route使用小结
  2. 【干货】计算机视觉中的数据预处理与模型训练技巧总结
  3. CRUD-员工列表 大体流程
  4. IDEA 15款神级良心插件强烈推荐收藏,提高一倍工作效率!
  5. oracle11g sp 1503,Oracle11g操作ASM权限问题
  6. Fiddler使用方法简介
  7. [Kafka与Spark集成系列三] Spark编程模型
  8. 基于java 工单管理_实训任务工单1-2(编写规范Java代码) 实训任务工单1-2(编写规范Java代码).docx_学小易找答案...
  9. LeetCode 553. 最优除法(数学)
  10. 富文本处理NSMutableAttributedString
  11. java学习 - 函数
  12. [图形图像]一次光线追踪的尝试
  13. Android 中自定义控件和属性(attr.xml,declare-styleable,TypedArray)的方法和使用 一
  14. 极大强连通分量的Tarjan算法
  15. Android期末项目2048小游戏
  16. stata计量之前数据清洗的必备步骤
  17. 数学建模国赛latex写作模板
  18. 【论文学习】《One-shot Voice Conversion by Separating Speaker and Content Representations with IN》
  19. Vue前端页面开发之vue2.0脚手架搭建
  20. 昨日皇者——Symbian(塞班)

热门文章

  1. Erratic Expansion UVA - 12627
  2. 数据分析和数据可视化网站资源
  3. 了解Java线程优先级,更要知道对应操作系统的优先级,不然会踩坑
  4. 计算机主板上的ide,主板上的IDE是什么?作用?
  5. Microsoft Word文档损坏,如何快速修复?一招解决文档修复
  6. 2000万直播数据看20万游戏主播能否月入100万
  7. 五色石FCS周报 2019.6.3-2019.6.9
  8. IEC 61131-3编程标准(GBT 15969.3)
  9. 解决win10快捷键打开程序延时问题
  10. 安装oracle-- redhat-- ins_ctx.mk问题