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

2025-05-29 0 38

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

现在很多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捕捉截屏事件并展示截图效果

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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 iOS捕捉截屏事件并展示截图效果 https://www.kuaiidc.com/89029.html

相关文章

发表评论
暂无评论