iOS 获取系统截屏事件
2018-12-24 本文已影响0人
书写不简单
1.系统通知名称
UIApplicationUserDidTakeScreenshotNotification
以下是系统给出的说明
// 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);
2.注册监听对象
你可以写在想要的页面控制器或者直接放在AppDelegate中
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(userDidTakeScreenshot:)
name:UIApplicationUserDidTakeScreenshotNotification
object:nil];
处理方法:
#pragma mark - 用户截屏通知事件
- (void)userDidTakeScreenshot:(NSNotification *)notification {
NSLog(@"检测到截屏");
// 手动截取当前屏幕图片
UIImage *mainScreenshot = [[UIApplication sharedApplication].keyWindow yg_screenshot];
/*
自定义处理方法的代码,随意吧
*/
//添加显示
UIImageView *imgvPhoto = [[UIImageView alloc]initWithImage:image_];
imgvPhoto.frame = CGRectMake(self.window.frame.size.width/2, self.window.frame.size.height/2, self.window.frame.size.width/2, self.window.frame.size.height/2);
//添加显示
UIImageView *imgvPhoto = [[UIImageView alloc]initWithImage:mainScreenshot];
imgvPhoto.image = mainScreenshot;
imgvPhoto.frame = CGRectMake(kSCREEN_WIDTH * 1/6, kSCREEN_HEIGHT * 1/6, kSCREEN_WIDTH * 2/3, kSCREEN_HEIGHT * 2/3);
//添加边框
CALayer * layer = [imgvPhoto layer];
layer.borderColor = [
[UIColor clearColor] CGColor];
layer.borderWidth = 1.0f;
//
UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, kSCREEN_HEIGHT)];
backView.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.8];
UIButton *shareBtn = [UIButton buttonWithType:UIButtonTypeSystem];
shareBtn.titleLabel.font = [UIFont systemFontOfSize:17.0];
[shareBtn setTintColor:[UIColor whiteColor]];
shareBtn.frame = CGRectMake(kSCREEN_WIDTH/5,kSCREEN_HEIGHT ,kSCREEN_WIDTH*3/5,50);
[shareBtn.layer setMasksToBounds:YES];
[shareBtn.layer setBorderWidth:1];
shareBtn.layer.cornerRadius = 6;
[shareBtn setTitle:@"分享给好友" forState:UIControlStateNormal];
shareBtn.backgroundColor = [UIColor orangeColor];
[shareBtn addTarget:self action:@selector(shareBtn:) forControlEvents:UIControlEventTouchUpInside];
[backView addSubview:imgvPhoto];
[backView addSubview:shareBtn];
UIWindow *window = [UIApplication sharedApplication].keyWindow;
[window addSubview:backView];
[UIView animateWithDuration:1.0 animations:^{
imgvPhoto.transform = CGAffineTransformMakeScale(0.8, 0.8);
shareBtn.transform = CGAffineTransformMakeTranslation(0, -50);
}];
//3秒后消失
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[backView removeFromSuperview];
});
}
3.获取截屏图片UIImage对象
- (UIImage *)yg_screenshot {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [UIScreen mainScreen].scale);
// 这个函数主要用于处理layer层,并且在layer层添加图片等图层
如果贸然的添加图片 那么图片的边缘就会和背景图层有种分割感 不够自然
render的意思是渲染
所以它会使图片和背景图层在结合的地方更自然
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImageJPEGRepresentation(image, 0.5);//数值 0 - 1 之间
image = [UIImage imageWithData:imageData];
return image;
}
4.UIImageJPEGRepresentation 使用中存在的问题
iOS上有两种转化图片的的简单方法:
1丶UIImageJPEGRepresentation(<#UIImage * _Nonnull image#>, <#CGFloat compressionQuality#>)
参数1:图片,参数2:压缩系数
2丶UIImagePNGRepresentation(<#UIImage * _Nonnull image#>)
参数:图片
UIImageJPEGRepresentation 压缩后图片较小图片质量也无较大差异:
UIImagePNGRepresentation 压缩图片的图片较大;
日常工作中推荐使用的是UIImageJPEGRepresentation图压缩后节省内存,减少避免图片过多照成的卡顿现象。
当然如果对图片画质有极高的要求的话,还是使用UIImagePNGRepresentation。
使用UIImageJPEGRepresentation遇到的问题:
///< NSData *data = UIImageJPEGRepresentation(image, 1);偶然发现使用UIImageJPEGRepresentation 一些无背景的白色图案经过压缩之后会变成白色背景无图案的图片,因为像素低,再经压缩后,就会导致图片失真, 成为白底无图案的图片(这种情况应该不常见,但确实存在);