推荐一个第三方好用的框架: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. MySQL中show语法使用总结

    MySQL中 show 语法的使用: 先查看MySQL版本信息: mysql> select version(); +------------+ | version() | +--------- ...

  2. 正则表达式(Java版整理)

    基础 元字符 代码 说明 . 匹配除换行符以外的任意字符 \w 匹配字母或数字或下划线或汉字 \s 匹配任意的空白符 \d 匹配数字 ^ 匹配字符串的开始 $ 匹配字符串的结束 \b 匹配字符串的结束 ...

  3. 关于ARM NEON学习的一些资料

    在对基于ARM-v7处理器及以上的程序进行优化时,可以使用neon优化技术来加速程序.不过搞这个的人比较少,所以网上有用的资料很稀少.我翻了半天国内国外的博客,发现还是ARM公司的帮助网站最有用: h ...

  4. 最好的Java和Android开发IDE---IntelliJ IDEA使用技巧

    转载请注明网址:http//:www.cnblogs.com/JohnTsai 以前一直使用的是Eclipse,听别人介绍说IDEA非常不错,也为了以后转Android studio铺垫下.就开始尝试 ...

  5. NSIS 资料

    官方 http://nsis.sourceforge.net/Main_Page NSIS官方插件全集 http://az.eliang.com/aq_2013041703.html NSIS 衿华客 ...

  6. 详解BarTender符号体系特殊选项之“行数”

    上面两篇文章小编和大家分享了BarTender符号体系特殊选项中的“行高”和“列”.此外,某些二维 (2D) 符号体系的结构为多个信息行,每一行看上去都像一个非常窄的条形码. 例如,以下图像是含 3 ...

  7. PDF文件转换成Excel表格的操作技巧

    我们都知道2007以上版本的Office文档,是可以直接将文档转存为PDF格式文档的.那么反过来,PDF文档可以转换成其他格式的文档吗?这是大家都比较好奇的话题.如果可以以其他格式进行保存,就可以极大 ...

  8. myeclipse安装jad反编译插件

    有时候想深入底层看jar包封装的源代码,但是打不开.这就需要配置反编译插件: 1:准备原材料 jad.exe + net.sf.jadclipse_3.3.0.jar 下载目录: jad.exe : ...

  9. mysql中json_object函数的使用?

    需求说明: 今天看了json_object函数的使用,在此记录下使用过程 操作过程: 1.使用json_object函数将一个键值对列表转换成json对象 mysql> select json_ ...

  10. vs2012更改默认开发环境

    1.在菜单栏里找到“Tools”(工具),选择下面的“Import and Export Settings”(导入和导出设置),如图1所示:          图1 2.弹出如下界面,按提示选择你需要 ...