2012-03-21 35 views
4

嗨我有一個iOS應用程序,可以打印UITableView。但是,只顯示了所顯示的tableview的一部分。有沒有辦法打印整個tableview,即使它還沒有繪製?打印具有屏幕內容的UITableView?

這裏是我用打印的代碼:

UIGraphicsBeginImageContext(self.mainTableView.contentSize); 
[self.mainTableView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

UIPrintInteractionController *printer = [UIPrintInteractionController sharedPrintController]; 
printer.printingItem = viewImage; 
UIPrintInfo *info = [UIPrintInfo printInfo]; 
printer.printInfo = info; 
UIPrintInteractionCompletionHandler completionHandler = 
^(UIPrintInteractionController *pic, BOOL completed, NSError *error) { 
    if (!completed && error) 
     NSLog(@"FAILED! due to error in domain %@ with error code %u: %@", 
       error.domain, error.code, [error localizedDescription]); 
}; 
UIButton * printButton = (UIButton *)sender; 
if(UIUserInterfaceIdiomPad == [[UIDevice currentDevice] userInterfaceIdiom]){ 
    [printer presentFromRect:printButton.frame inView:self.view animated:YES completionHandler:completionHandler]; 
} else { 
    [printer presentAnimated:YES completionHandler:completionHandler]; 
} 

回答

3

有這個問題多次討論。舉兩個例子:

SO: Screenshot from a UITableView

SO: How to take an iPhone screenshot of entire view including parts off screen

的問題是,UITableViews重用他們的細胞。 我認爲最常見的方法是:

  1. 滾動編程到您的UITableView的不同部分,以不同的屏幕截圖,並將它們重新合併在一起。
  2. 調整UITableView的大小,使其大小適合所有單元格。警告:這很快就會導致內存問題(不再使用單元格),因此只是爲了屏幕截圖的目的,並且如果您的單元格數量不是那麼大。