2012-06-16 25 views
2

下面的代碼工作正常,但只抓住在您的屏幕上可見的UIView。 我該如何獲取一個當前沒有顯示的UIView?捕獲非活動UIView作爲UIImage

謝謝!

//Take a screenshot of the view... 
UIGraphicsBeginImageContext(View_1.frame.size); 
    [View_1.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *View_1 = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
UIImageWriteToSavedPhotosAlbum(View_1, nil, nil, nil); 

//...and attach it as an image to the email 
NSData *myData3 = UIImagePNGRepresentation(View_1); 
[picker addAttachmentData:myData3 mimeType:@"image/png" fileName:@"screenshot2"]; 

回答

2

如果你的意思是在view.hidden = YES;隱象,那麼你將無法繪製,而不是隱藏它,你可以從超級視圖後,將其刪除,或繪圖之前甚至稱view.hidden = NO;,然後view.hidden = YES;已經繪

例如

//Take a screenshot of the view... 
UIGraphicsBeginImageContext(View_1.frame.size); 
//Set it to visible 
View_1.hidden = NO; 
[View_1.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *View_1 = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
UIImageWriteToSavedPhotosAlbum(View_1, nil, nil, nil); 
//Hide it again 
View_1.hidden = YES; 
//...and attach it as an image to the email 
NSData *myData3 = UIImagePNGRepresentation(View_1); 
[picker addAttachmentData:myData3 mimeType:@"image/png" fileName:@"screenshot2"]; 
+0

你是個天才!它被隱藏設置隱藏...非常感謝您的幫助。 :-) – jeddi

+0

雖然與問題的標題不太相關......你知道是否可以向附件添加多於一個的圖像? – jeddi