2014-10-22 111 views
1

我使用下面的代碼拍攝一個視圖的快照。它適用於大多數情況,但不適用於包含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

回答

1

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

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

謝謝你。對不起,接受答案已經太晚了。它適用於iOS7和iOS9,我沒有在兩個系統版本上測試過其他設備。 – KudoCC 2016-06-24 02:23:28

0

設置不透明= 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