拍摄快照的视图包含UIDatePicker

问题描述:

我使用下面的代码拍摄一个视图的快照。它适用于大多数情况,但不适用于包含UIDatePicker的视图。拍摄快照的视图包含UIDatePicker

UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0.0); 
    [view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *snapshot = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

的观点是:

enter image description here

它生成的图像看起来奇怪:

enter image description here

谁能告诉我,为什么发生这种情况以及如何采取的快照视图包含UIDatePicker

在此先感谢。

+0

你解决这个问题? – demon9733 2015-09-23 10:03:21

+0

你解决了这个问题吗? – 2016-06-23 20:25:45

+0

@ demon9733 user1390486的回答是有效的。 – KudoCC 2016-06-24 02:22:12

而不是使用层的renderInContext方法,使用:

[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES]; 
+0

谢谢你。对不起,接受答案已经太晚了。它适用于iOS7和iOS9,我没有在两个系统版本上测试过其他设备。 – KudoCC 2016-06-24 02:23:28

设置不透明= NO在调用UIGraphicsBeginImageContextWithOptions()

+0

它不起作用。 'CGContextRef context = UIGraphicsGetCurrentContext();' 'CGContextSetAlpha(context,1.0);' – KudoCC 2014-10-23 07:32:45

+0

适用于我。 OS版本? – 2014-10-23 07:52:14

+0

iOS 7.1.1。如果你没有将不透明设置为NO,你是否会遇到和我一样的问题。 – KudoCC 2014-10-23 08:13:31