iOS 用户截屏

本文介绍了一个iOS应用如何监听用户的截屏操作,并提供了一个全屏截图的方法实现。针对iOS7及之后版本的变化进行了特别说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

AppDelegate.m

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    //ios7之后,监听用户截屏通知,将会在截图完成之后调用 userDidTakeScreenshot:方法
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(userDidTakeScreenshot:)
                                                 name:UIApplicationUserDidTakeScreenshotNotification object:nil];
    return YES;
}

- (void)userDidTakeScreenshot:(NSNotification *)notification {
    //截图完成之后,相关操作。。。
    
}

/**
 *  全屏截图
 *
 */
+(UIImage *)fullScreenShots{
    UIWindow *screenWindow = [[UIApplication sharedApplication] keyWindow];
    UIGraphicsBeginImageContext(screenWindow.frame.size);
    [screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return viewImage;
}

/**
   在iOS7之前, 如果用户截屏,系统会自动取消屏幕上的所有 touch 事件,那么我们就可以检测这个方法的调用,然后加载本地最新图片再加以判断来实现我们的目的。但在 iOS 7 之后,截屏不再会取消屏幕的 touch 事件,所以导致了 Snapchat 和 Facebook Poke 之类的应用在 iOS 7 刚发布时依赖于系统这个行为的功能受到影响。
 */
-(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
}

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值