IOS 检测截图并获取截图图片(原理介绍)

之前用Swift给大家分享过一篇截图分享的文章:https://www.jianshu.com/p/51ae56f1eb63。现在给大家用OC代码小小的翻译一下。

先简单介绍一下,大家使用“支付宝”等APP的时候,只要截图,页面就会弹出提示。那么APP是如何知道我们进行了截图的操作呢?这种牛逼哄哄的效果其实很简单,因为在iOS7以后苹果为大家提供了一个获取截图的通知,会在截图后调用:

OC:UIApplicationUserWillTakeScreenshotNotification
Swift:UIApplicationUserDidTakeScreenshot

其实有了通知,接下来就很简单了。有的同学会考虑获得截图的通知后调用系统的相册,并且获取最后一张图片,这个方法可以实现,不过有很多缺点。实现成本先不用说,万一用户不同意获取相册岂不是傻眼了吗。所以,最简单的方法就是直接截取当前屏幕的图片,既简单又方便,上代码:

#import "ViewController.h"

@interface ViewController ()

/// 添加裁剪显示的图片
@property (nonatomic, strong) UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupViewController];
}

-(void)setupViewController {
    /// 随便一张默认的占位图(同学们不需要的话可以直接删除)
    UIImageView *testImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image"]];
    testImageView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    [self.view addSubview:testImageView];
    
    /// 添加裁剪显示的图片
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 200, 200, 300)];
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    self.imageView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.imageView];
    
    /// 添加通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userDidTakeScreenshot) name:UIApplicationUserDidTakeScreenshotNotification object:nil];
}

-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - 截屏通知
-(void)userDidTakeScreenshot {
    self.imageView.image = [self takeScreenshot];
}

#pragma mark - 截取当前屏幕
-(UIImage *)takeScreenshot {
    CGSize imageSize = CGSizeZero;
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

    if (UIInterfaceOrientationIsPortrait(orientation)) {
        imageSize = screenSize;
    } else {
        imageSize = CGSizeMake(screenSize.height, screenSize.width);
    }
    
    UIGraphicsBeginImageContextWithOptions(imageSize, false, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    if (context) {
        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);
            
            if (orientation == UIInterfaceOrientationLandscapeLeft) {
                CGContextRotateCTM(context, M_PI_4);
                CGContextTranslateCTM(context, 0, -imageSize.width);
            } else if (orientation == UIInterfaceOrientationLandscapeRight) {
                CGContextRotateCTM(context, - M_PI_2);
                CGContextTranslateCTM(context, -imageSize.height, 0);
            } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
                CGContextRotateCTM(context, M_PI);
                CGContextTranslateCTM(context, -imageSize.width, -imageSize.height);
            }
            
            if ([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
                [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];
            } else {
                [window.layer renderInContext:context];
            }
            
            CGContextRestoreGState(context);
        }
    }
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

@end

最后还是需要提醒一下各位同学,使用时建议大家将方法放置到Base控制器,这样就可以方便的在APP的任意位置获取截图,并且统一分享。喜欢的同学可以点一个赞哈。

推荐阅读更多精彩内容