IOS 检测截图并获取截图图片(原理详情)

作者 : 开心源码 本文共3144个字,预计阅读时间需要8分钟 发布时间: 2022-05-12 共150人阅读

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

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

OC:UIApplicationUserWillTakeScreenshotNotificationSwift: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的任意位置获取截图,并且统一分享。喜欢的同学可以点一个赞哈。

说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » IOS 检测截图并获取截图图片(原理详情)

发表回复