IOS截屏,View截图的基本方法

来源:互联网 发布:android 视频直播源码 编辑:程序博客网 时间:2024/05/10 00:51

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

来源:SDScreenshotCapture

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)  

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. UIImage *getImageWithFullScreenshot(void)  
  2. {  
  3.     // Source (Under MIT License): https://github.com/shinydevelopment/SDScreenshotCapture/blob/master/SDScreenshotCapture/SDScreenshotCapture.m#L35  
  4.       
  5.     BOOL ignoreOrientation = SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0");  
  6.       
  7.     UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;  
  8.       
  9.     CGSize imageSize = CGSizeZero;  
  10.     if (UIInterfaceOrientationIsPortrait(orientation) || ignoreOrientation)  
  11.         imageSize = [UIScreen mainScreen].bounds.size;  
  12.     else  
  13.         imageSize = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);  
  14.       
  15.     UIGraphicsBeginImageContextWithOptions(imageSize, NO, [UIScreen mainScreen].scale);  
  16.     CGContextRef context = UIGraphicsGetCurrentContext();  
  17.       
  18.     for (UIWindow *window in [[UIApplication sharedApplication] windows])  
  19.     {  
  20.         CGContextSaveGState(context);  
  21.         CGContextTranslateCTM(context, window.center.x, window.center.y);  
  22.         CGContextConcatCTM(context, window.transform);  
  23.         CGContextTranslateCTM(context, -window.bounds.size.width * window.layer.anchorPoint.x, -window.bounds.size.height * window.layer.anchorPoint.y);  
  24.           
  25.         // Correct for the screen orientation  
  26.         if(!ignoreOrientation)  
  27.         {  
  28.             if(orientation == UIInterfaceOrientationLandscapeLeft)  
  29.             {  
  30.                 CGContextRotateCTM(context, (CGFloat)M_PI_2);  
  31.                 CGContextTranslateCTM(context, 0, -imageSize.width);  
  32.             }  
  33.             else if(orientation == UIInterfaceOrientationLandscapeRight)  
  34.             {  
  35.                 CGContextRotateCTM(context, (CGFloat)-M_PI_2);  
  36.                 CGContextTranslateCTM(context, -imageSize.height0);  
  37.             }  
  38.             else if(orientation == UIInterfaceOrientationPortraitUpsideDown)  
  39.             {  
  40.                 CGContextRotateCTM(context, (CGFloat)M_PI);  
  41.                 CGContextTranslateCTM(context, -imageSize.width, -imageSize.height);  
  42.             }  
  43.         }  
  44.           
  45.         if([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])  
  46.             [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:NO];  
  47.         else  
  48.             [window.layer renderInContext:UIGraphicsGetCurrentContext()];  
  49.           
  50.         CGContextRestoreGState(context);  
  51.     }  
  52.       
  53.     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();  
  54.       
  55.     UIGraphicsEndImageContext();  
  56.       
  57.     return image;  
  58. }  

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

h文件

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface UIWindow (Category)  
  4.   
  5. - (UIImage *)screenshot;  
  6. - (UIImage *)screenshotWithRect:(CGRect)rect;  
  7.   
  8. @end  

m文件

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. #import "UIWindow+Category.h"  
  2.   
  3. @implementation UIWindow (Category)  
  4.   
  5. - (UIImage *)screenshot  
  6. {  
  7.     return [self screenshotWithRect:self.bounds];  
  8. }  
  9.   
  10. - (UIImage *)screenshotWithRect:(CGRect)rect  
  11. {  
  12.     // Source (Under MIT License): https://github.com/shinydevelopment/SDScreenshotCapture/blob/master/SDScreenshotCapture/SDScreenshotCapture.m#L35  
  13.       
  14.     BOOL ignoreOrientation = SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0");  
  15.       
  16.     UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;  
  17.       
  18.     CGSize imageSize = CGSizeZero;  
  19.     CGFloat width = rect.size.width, height = rect.size.height;  
  20.     CGFloat x = rect.origin.x, y = rect.origin.y;  
  21.       
  22. //    imageSize = CGSizeMake(width, height);  
  23. //    UIGraphicsBeginImageContextWithOptions(imageSize, NO, [UIScreen mainScreen].scale);  
  24.     if (UIInterfaceOrientationIsPortrait(orientation) || ignoreOrientation)  
  25.     {  
  26.         //imageSize = [UIScreen mainScreen].bounds.size;  
  27.         imageSize = CGSizeMake(width, height);  
  28.         x = rect.origin.x, y = rect.origin.y;  
  29.     }  
  30.     else  
  31.     {  
  32.         //imageSize = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);  
  33.         imageSize = CGSizeMake(height, width);  
  34.         x = rect.origin.y, y = rect.origin.x;  
  35.     }  
  36.       
  37.     UIGraphicsBeginImageContextWithOptions(imageSize, NO, [UIScreen mainScreen].scale);  
  38.     CGContextRef context = UIGraphicsGetCurrentContext();  
  39.     CGContextSaveGState(context);  
  40.     CGContextTranslateCTM(context, self.center.xself.center.y);  
  41.     CGContextConcatCTM(context, self.transform);  
  42.     CGContextTranslateCTM(context, -self.bounds.size.width * self.layer.anchorPoint.x, -self.bounds.size.height * self.layer.anchorPoint.y);  
  43.       
  44.     // Correct for the screen orientation  
  45.     if(!ignoreOrientation)  
  46.     {  
  47.         if(orientation == UIInterfaceOrientationLandscapeLeft)  
  48.         {  
  49.             CGContextRotateCTM(context, (CGFloat)M_PI_2);  
  50.             CGContextTranslateCTM(context, 0, -self.bounds.size.height);  
  51.             CGContextTranslateCTM(context, -x, y);  
  52.         }  
  53.         else if(orientation == UIInterfaceOrientationLandscapeRight)  
  54.         {  
  55.             CGContextRotateCTM(context, (CGFloat)-M_PI_2);  
  56.             CGContextTranslateCTM(context, -self.bounds.size.width0);  
  57.             CGContextTranslateCTM(context, x, -y);  
  58.         }  
  59.         else if(orientation == UIInterfaceOrientationPortraitUpsideDown)  
  60.         {  
  61.             CGContextRotateCTM(context, (CGFloat)M_PI);  
  62.             CGContextTranslateCTM(context, -self.bounds.size.height, -self.bounds.size.width);  
  63.             CGContextTranslateCTM(context, x, y);  
  64.         }  
  65.         else  
  66.         {  
  67.             CGContextTranslateCTM(context, -x, -y);  
  68.         }  
  69.     }  
  70.     else  
  71.     {  
  72.         CGContextTranslateCTM(context, -x, -y);  
  73.     }  
  74.       
  75.     //[self layoutIfNeeded];  
  76.       
  77.     if([self respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])  
  78.         [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:NO];  
  79.     else  
  80.         [self.layer renderInContext:UIGraphicsGetCurrentContext()];  
  81.       
  82.     CGContextRestoreGState(context);  
  83.     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();  
  84.     UIGraphicsEndImageContext();  
  85.       
  86.     return image;  
  87. }  
  88.   
  89. @end  

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


View截图

h文件

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. @interface UIView (Screenshot)  
  2. - (UIImage *)screenshot;  
  3. - (UIImage *)screenshotWithRect:(CGRect)rect;  
  4. @end  

m文件

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. @implementation UIView (Screenshot)  
  2.   
  3. - (UIImage *)screenshot  
  4. {  
  5.     return [self screenshotWithRect:self.bounds];  
  6. }  
  7.   
  8. - (UIImage *)screenshotWithRect:(CGRect)rect;  
  9. {  
  10.     UIGraphicsBeginImageContextWithOptions(rect.sizeNO, [UIScreen mainScreen].scale);  
  11.   
  12.     CGContextRef context = UIGraphicsGetCurrentContext();  
  13.     if (context == NULL)  
  14.     {  
  15.         return nil;  
  16.     }  
  17.     CGContextSaveGState(context);  
  18.     CGContextTranslateCTM(context, -rect.origin.x, -rect.origin.y);  
  19.       
  20.     //[self layoutIfNeeded];  
  21.   
  22.     if( [self respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])  
  23.     {  
  24.         [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:NO];  
  25.     }  
  26.     else  
  27.     {  
  28.         [self.layer renderInContext:context];  
  29.     }  
  30.       
  31.     CGContextRestoreGState(context);  
  32.   
  33.     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();  
  34.     UIGraphicsEndImageContext();  
  35.       
  36. //    NSData *imageData = UIImageJPEGRepresentation(image, 1); // convert to jpeg  
  37. //    image = [UIImage imageWithData:imageData scale:[UIScreen mainScreen].scale];  
  38.       
  39.     return image;  
  40. }  

0 0