iOS开发 检测用户截图
2017-08-28 16:05
344 查看
一.介绍
iOS7提供一个崭新的监听用户截图的通知
二.代码
1.先写一个UIWindow的分类
.h文件
.m文件
2.注册通知
3.通知方法
iOS7提供一个崭新的监听用户截图的通知
// This notification is posted after the user takes a screenshot (for example by pressing both the home and lock screen buttons) UIKIT_EXTERN NSNotificationName const UIApplicationUserDidTakeScreenshotNotification NS_AVAILABLE_IOS(7_0);
二.代码
1.先写一个UIWindow的分类
.h文件
#import <Foundation/Foundation.h> @interface UIWindow (WLKit) - (UIImage *)takeScreenshot; @end
.m文件
#import "UIWindow+WLKit.h" @implementation UIWindow (WLKit) - (UIImage *)takeScreenshot { 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(); 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); 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 ([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
2.注册通知
// 截屏通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userDidTakeScreenshot:) name:UIApplicationUserDidTakeScreenshotNotification object:nil];
3.通知方法
#pragma mark - 截屏通知 - (void)userDidTakeScreenshot:(NSNotification *)not { NSLog(@"检测到截屏"); // 获取图片 UIImage *screenshotImage = self.view.window.takeScreenshot; // 保存图片 UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); }
相关文章推荐
- iOS开发-检测用户截屏, 并获取所截图片
- iOS 10 开发之权限Crash 相机相册等权限的使用、检测并引导用户开启权限
- iOS开发-检测用户截屏, 并获取所截图片
- iOS开发-检测用户截屏, 并获取所截图片
- iOS开发-检测用户截屏, 并获取所截图片
- iOS开发-检测用户截屏, 并获取所截图片
- iOS开发-检测用户截屏, 并获取所截图片
- IOS开发---获取用户当前地理坐标
- iOS开发_Objective-C_监听搜索时用户输入的拼音
- IOS开发笔记(Swift):利用开源类库实现检测更新功能
- iOS 开发APP更换用户头像问题的处理方式
- 以带数字的小圆点方式显示在桌面图标上面,通知用户有新的消息--iOS开发
- iOS开发:处理多点触摸与手势检测
- iOS通过iTunes search检测版本更新,并提示用户更新!
- Google Maps Engine Pro帮企业用户处理地理位置信息 iOS版还在开发中
- iOS通过iTunes search检测版本更新,并提示用户更新
- iOS开发之网络检测
- iOS 开发中用户记住账户,密码
- IOS开发之记录用户登陆状态