网站建设资讯

NEWS

网站建设资讯

iOS怎么实现捕捉截屏事件并展示截图效果

这篇文章主要为大家展示了“iOS怎么实现捕捉截屏事件并展示截图效果”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“iOS怎么实现捕捉截屏事件并展示截图效果”这篇文章吧。

创新互联是一家集网站建设,禹会企业网站建设,禹会品牌网站建设,网站定制,禹会网站建设报价,网络营销,网络优化,禹会网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

iOS7之后,苹果开放出一个通知:UIApplicationUserDidTakeScreenshotNotification,截屏时系统就会发出这个通知,需要你注册这个通知,就能捕捉到截屏图片。

下面的代码,实现的是用户截屏后,捕获到截屏图片,展示出来:

//注册截屏通知

[[NSNotificationCenter defaultCenter] addObserver:self    selector:@selector(getScreenShot:)    name:UIApplicationUserDidTakeScreenshotNotification object:nil];

截屏后捕捉到事件:

- (void)getScreenshot:(NSNotification *)notification{ NSLog(@"捕捉截屏事件"); //获取截屏图片 UIImage *image = [UIImage imageWithData:[self imageDataScreenShot]]; //显示图片 UIImageView *imgV = [[UIImageView alloc]initWithImage:image]; imgV.frame = [UIScreen mainScreen].bounds; UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_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(SCREEN_WIDTH/5,SCREEN_HEIGHT ,SCREEN_WIDTH*3/5,50); [shareBtn.layer setMasksToBounds:YES]; [shareBtn.layer setBorderWidth:1]; shareBtn.layer.cornerRadius = 6; [shareBtn setTitle:@"分享给好友" forState:UIControlStateNormal]; shareBtn.backgroundColor = [SouFunIMUtilityHelper colorWithHexString:@"#B22222"]; [shareBtn addTarget:self action:@selector(shareBtn:) forControlEvents:UIControlEventTouchUpInside]; [backView addSubview:imgV]; [backView addSubview:shareBtn]; UIWindow *window = [UIApplication sharedApplication].keyWindow; [window addSubview:backView]; [UIView animateWithDuration:1.0 animations:^{  imgV.transform = CGAffineTransformMakeScale(0.8, 0.8);  shareBtn.transform = CGAffineTransformMakeTranslation(0, -50); }]; //3秒后消失 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{  [backView removeFromSuperview]; });}

获取截屏图片data:

- (NSData *)imageDataScreenShot{ CGSize imageSize = CGSizeZero; imageSize = [UIScreen mainScreen].bounds.size; UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0); CGContextRef context = UIGraphicsGetCurrentContext(); 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 ([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])  {   [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];  }  else  {   [window.layer renderInContext:context];  }  CGContextRestoreGState(context); } UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return UIImagePNGRepresentation(image);}

按钮点击事件:

-(void)shareBtn:(UIButton *)sender{/* 分享代码*/}

以上是“iOS怎么实现捕捉截屏事件并展示截图效果”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


分享标题:iOS怎么实现捕捉截屏事件并展示截图效果
网页链接:http://cdweb.net/article/psisjp.html