摩拜单车、微信的截屏就做的比较人性化。
现在很多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{
/*
分享代码
*/
}
|
以上就是截屏后的事例代码,最后附上效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
相关文章
猜你喜欢
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-06-04 90
-
2025-06-04 32
-
2025-05-25 50
-
2025-05-25 31
-
2025-05-29 89
热门评论


