iOS - 截屏,view截图的基本方法
推荐一个第三方好用的框架: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截图的基本方法的更多相关文章
- iOS截屏并修改截图然后分享的功能实现
一. 实现的效果类似微博的截图分享 不仅截图分享的时候还进行图片的修改,增加自己的二维码 二.实现方式 苹果在ios7之后提供了一个新的通知类型:UIApplicationUserDidTakeScr ...
- iOS截屏方法
//获取屏幕截屏方法 - (UIImage *)capture { // 创建一个context UIGraphicsBeginImageContextWithOptions(self.view.bo ...
- iOS 截屏分享(包含状态栏与不包含状态栏)
iOS8以上的新方法PhotoKit 监听截图相册变化,取最后一张图片:http://www.hangge.com/blog/cache/detail_1515.html PhotoKit 获取本机相 ...
- ios截屏代码[转]
http://www.cnblogs.com/chenxiangxi/p/3547974.html 这位博主的连接中将ios自定义大小位置的截屏代码写的很不错,马上就能用的方法,对于只想马上用的程序员 ...
- iOS截屏代码
转载自:http://m.open-open.com/m/code/view/1420469506375 1.普通界面 /** *截图功能 */ -(void)screenShot{ UIGraphi ...
- iOS截屏保存至相册
#pragma mark 截屏并保存至相册 -(void)screenShotsComplete:(void(^)(UIImage * img)) complete { CGSize imageSiz ...
- iOS截屏功能
代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // ...
- IOS 截屏(保存到相册中)
@interface NJViewController () /** * 点击截屏按钮 */ - (IBAction)captureView:(UIButton *)sender; /** * 白色v ...
- ios截屏事件监听
目的:实现截屏反馈,类似支付宝的截屏上传反馈功能. 1.注册全局通知,在Appdelegate中注册截屏监听通知 - (void)registNotification{ [[NSNotificatio ...
随机推荐
- window.location.href 跳转失败
我恨这个问题,因为困扰已久,做为一个不称职的半开发人员,对前端非常不熟,程度仅限于alert调试的水平 这个问题似乎是IE的一个傻逼哄哄的BUG引起的,在AJAX盛行的今天,当在返回success状态 ...
- FTP服务器的配置与实现
一.准备工作 实验目的:完成FTP服务器的配置,并能熟练操作. 环境搭建: 虚拟机 vmware workstation windows2003镜像文件 Serv-U 主机 二.步骤 1,在虚拟机中 ...
- PHP + Smarty + html5 构建Wap应用
一 简介 Smarty是一个PHP编写的模板引擎(template engine),主要用于构建web应用程序的表示层.Smarty的主页是http://www.smarty.net/down ...
- Java如何在正则表达式中匹配重复单词?
在Java编程中,如何在正则表达式中匹配重复单词? 以下示例显示了如何使用regex.Matcher类的p.matcher()方法和m.group()方法在正则表达式中搜索重复的单词. package ...
- HTML5 Canvas火焰效果 像火球发射一样
Canvas是HTML5中非常重要而且有用的东西,我们可以在Canvas上绘制任意的元素,就像你制作Flash一样.今天我们就在Canvas上来制作一款火焰发射的效果.就像古代的火球炮一样,而且可以在 ...
- UITableView 顶部能够放大的图片
UITableView 顶部能够放大的图片 现在有挺多的应用在 UITableView 顶部加入图片,通过拖拽 UITableView 来实现图片的放大. 对比一下腾讯出品的两款App QQ:可展示更 ...
- memcached能获取所有的key吗
memcached能获取所有的key吗 Memcache 查看列出所有key方法 Memcached中获取所有的key 特别要注意:memcached保存的值需要序列化,否则是无法保存的,而且是不会报 ...
- Java写 插入 选择 冒泡 快排
/** * Created by wushuang on 2014/11/19. */ public class SortTest { @Test public void mainTest() { i ...
- DIV背景图片定位问题
<div class="custom-topNavigation_shadow"> </div> 正确写法 .custom-topNavigation_ ...
- Redis Crackit漏洞利用和防护
注意:本文只是阐述该漏洞的利用方式和如何预防.根据职业道德和<中华人民共和国计算机信息系统安全保护条例>,如果发现的别人的漏洞,千万不要轻易入侵,这个是明确的违法的哦!!! 目前Redis ...