如何在不打扰用户的情况下保存部分图形上下文?
我的情况是这样的,我想以编程方式打印用户可见的当前视图的屏幕并将其保存到相册中。问题是我不希望层次结构中的所有视图都可见。我的计划是:
- 制作视图层次结构的副本。
- 隐藏不感兴趣的子视图。
- 获取打印屏幕。
- 将其保存到相册中。
问题是我陷入了第一点。由于 UIView
没有实现 NSCopying
协议,我无法对视图层次结构进行深层复制。我尝试归档和取消归档视图,但这仅在桌面上可用。
据我所知,这里有两个选择:
a) 在所有视图和子视图上实现 NSCopying 协议。 b) 淡出覆盖整个屏幕的白色视图(有点像手动打印屏幕时的闪光效果),在那一刻我隐藏了我想要的视图。
还有其他方法可以解决我错过的这个问题吗?
编辑:
UIGraphicsBeginImageContext([[wordManager mainWorkViewController] view].frame.size);
[[[[wordManager mainWorkViewController] view] layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
最诚挚的问候 //Abeansits
My situation is like this, I want to programmatically take a print screen of the current view visible to the user and save this to the photo album. The problem is that I don't want all of the views in the hierarchy to be visible. My plan was to:
- Make a copy of the view hierarchy.
- Hide the subviews which were not interesting.
- Take the print screen.
- Save it to the photo album.
The problem is that I'm stuck on the first point. Since UIView
does not implement the NSCopying
protocol I can't make a deep copy of the view hierarchy. I tried archiving and unarchiving the views but this was only available on the dektop.
As I see it there there are two options here:
a) Implement the NSCopying protocol on all the views and subviews.
b) Fade a white view covering the whole screen (sort of like the flash-effect when you take a print screen manually) and in that instant I hide the views I want.
Is there some other way of approaching this problem that I have missed?
Edit:
UIGraphicsBeginImageContext([[wordManager mainWorkViewController] view].frame.size);
[[[[wordManager mainWorkViewController] view] layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Best regards
//Abeansits
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,不幸的是没有人能够就此事提供任何帮助。
我如何解决这个问题:
我删除了打印屏幕中不需要的元素。我使用动画来做到这一点,这样就不会出现图形闪烁。最后看起来还不错,可惜我从来没有解决最初的问题。
如果有人发现,请发布修复程序。
最好的问候
//Abeansits
So, unfortunately no one was able to provide any help on this matter.
How I solved the problem:
I removed the elements which I did not want in the print screen. I did this using animations so there would be no graphical flicker. In the end it looked pretty good, to bad I never solved the original problem.
Please post a fix if any one finds one.
Best regards
//Abeansits