推荐一个第三方好用的框架: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#L35 BOOL 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;
else
imageSize = 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 orientation
if(!ignoreOrientation)
{
if(orientation == UIInterfaceOrientationLandscapeLeft)
{
CGContextRotateCTM(context, (CGFloat)M_PI_2);
CGContextTranslateCTM(context, , -imageSize.width);
}
else if(orientation == UIInterfaceOrientationLandscapeRight)
{
CGContextRotateCTM(context, (CGFloat)-M_PI_2);
CGContextTranslateCTM(context, -imageSize.height, );
}
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#L35 BOOL 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 orientation
if(!ignoreOrientation)
{
if(orientation == UIInterfaceOrientationLandscapeLeft)
{
CGContextRotateCTM(context, (CGFloat)M_PI_2);
CGContextTranslateCTM(context, , -self.bounds.size.height);
CGContextTranslateCTM(context, -x, y);
}
else if(orientation == UIInterfaceOrientationLandscapeRight)
{
CGContextRotateCTM(context, (CGFloat)-M_PI_2);
CGContextTranslateCTM(context, -self.bounds.size.width, );
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截屏并修改截图然后分享的功能实现

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

  2. iOS截屏方法

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

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

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

  4. ios截屏代码[转]

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

  5. iOS截屏代码

    转载自:http://m.open-open.com/m/code/view/1420469506375 1.普通界面 /** *截图功能 */ -(void)screenShot{ UIGraphi ...

  6. iOS截屏保存至相册

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

  7. iOS截屏功能

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

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

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

  9. ios截屏事件监听

    目的:实现截屏反馈,类似支付宝的截屏上传反馈功能. 1.注册全局通知,在Appdelegate中注册截屏监听通知 - (void)registNotification{ [[NSNotificatio ...

随机推荐

  1. C++自定义异常类

    代码样例: #include <iostream> using namespace std; class illegalParameterValue { public: illegalPa ...

  2. SAP OBYC自动记账的实例说明 +VALUE STRING

    对Value String定义:定义了一系列的步骤优先顺序,每一个步骤都连接到不同的过账事务码,而这个顺序本身就称作价值串.价值串你可以看作是一种记账的规则,为物料移动或者发票校验包含了一系列的科目分 ...

  3. Java数组列表反转

    在Java中,如何反转数组列表中的元素? 以下示例使用Collections.reverse(ArrayList)方法反转数组列表中的元素. package com.yiibai; public cl ...

  4. e565. 关闭的时候隐藏窗口

    By default, when the close button on a frame is clicked, nothing happens. This example shows how to ...

  5. 如何解析oracle执行计划

    要执行任何SQL语句,Oracle 必须推导出一个“执行计划”.查询的执行计划是 Oracle 将如何实现数据的检索,以满足给定 SQL 语句的描述.它只不过是其中包含的步骤及它们之间关系的顺序树.执 ...

  6. PHP上传原理及操作实现

    关于PHP上传文件的函数类库,网上有许多封装很完善,大家直接拿来用就可以. 本文章只是说下关于上传原理和简单的上传操作,老鸟就无视了哈^_^~ 还有一些安全性判断比如:服务端限制能接收图片类型的文件, ...

  7. 【WP8】WP8调用官方API使用LED灯

    在WP7中没有相关的API可以直接使用摄像头的LED等,只能通过录像时打开LED等来使用,在WP8中添加了相关的调用接口,可以方便的使用LED灯,并且支持后台,废话不多说,直接上代码 1.在 WMAp ...

  8. SharePoint PowerShell使用Backup-SPSite指令来备份网站集

    备份网站集: Backup-SPSite -Identity http://win2012sp2013:1000/ -Path "C:\KenmuTemp\Test File\Temp\si ...

  9. linux cfs调度器_理论模型

    参考资料:<调度器笔记>Kevin.Liu <Linux kernel development> <深入Linux内核架构> version: 2.6.32.9 下 ...

  10. rlwrap安装报错You need the GNU readline 解决方法

    首先大家肯定知道rlwrap是干什么的? 在linux以及unix中,sqlplus的上下左右.回退无法使用,会出现乱码情况.而rlwrap这个软件就是用来解决这个的.   这个错误曾经困扰我很久很久 ...