iOS捕捉截屏事件并展示截图效果

时间:2022-02-26 20:02:07

摩拜单车、微信的截屏就做的比较人性化。

现在很多app开始支持用户截屏后,主动获取截图并弹出分享视图,这样用户就不用去相册去找了,感觉体验不错,今天就分享一下 截屏开发的心得,希望能帮助ios的朋友。

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

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

//注册截屏通知

?
1
2
3
[[nsnotificationcenter defaultcenter] addobserver:self
   selector:@selector(getscreenshot:)
   name:uiapplicationuserdidtakescreenshotnotification object:nil];

截屏后捕捉到事件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
- (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:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
- (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);
}

按钮点击事件:

?
1
2
3
4
5
6
-(void)sharebtn:(uibutton *)sender{
 
/*
 分享代码
*/
}

以上就是截屏后的事例代码,最后附上效果图:

iOS捕捉截屏事件并展示截图效果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/SL_ideas/article/details/73332058