IOS截屏,View截图的基本方法
来源:互联网 发布:淘宝注册公司 编辑:程序博客网 时间:2024/04/27 20:55
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#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, 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#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, 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;}
2 0
- IOS截屏,View截图的基本方法
- IOS截屏,View截图的基本方法
- IOS截屏,View截图的基本方法
- IOS VIEW截图并保存为PNG的方法
- iOS对某一个view截图方法
- 获取某个view 的 截图的方法
- IOS页面截图的方法
- view 截图方法
- Mac截图基本方法
- ios 获取一个view上的图片,iphone混合图片 合成图片 任意范围截图 截屏
- iOS 快速获取屏幕截图的方法
- iOS截图方法
- Mac截图基本方法a
- android关于View的截图
- android关于View的截图
- 【Android】获取View的截图
- 【Android】获取View的截图
- Android屏幕截图之View方法
- Android实现推送方式解决方案
- Windows下Python开发环境搭建及示例
- ios模拟器不弹出软键盘,但是能输入
- ASP.NET MVC 5 Authentication Breakdown : Part Deux
- iOS关于本地推送通知的简单用法
- IOS截屏,View截图的基本方法
- double类型保留两位小数的方法
- xmanager 连接 centos 的 桌面
- 深入理解js闭包
- 蓝天碧水中国梦-总览(1)-中国发电情况
- 不成熟的五个特征
- Evaluate Reverse Polish Notation
- 日期在String和Date类型转换;ParsePosition,formatter.parse,java.sql.Timestamp
- C#换肤笔记