If you don't have much to say, go directly to the code.
1 // 2 // MainViewController. m 3 // TableViewSreenShot 4 // 5 // Created by ChenJungang on 14/11/8. 6 // Copyright (c) 2014 ChenJungang. all rights reserved. 7 // 8 9 # import "MainViewController. h "10 # import" MainCell. h "11 12 # define kTableViewRowCount 3013 # define kTableViewHeight 7614 15 @ interface MainViewController () 16 @ property (strong, nonatomic) IBOutlet UITableView * tableView; 17 @ end18 19 @ Implementation MainViewController20 21-(void) viewDidLoad {22 [super viewDidLoad]; 23 // Do any additional setup after loading the view from its nib.24 self. title = @ "screen shot"; 25 self. navigationItem. rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle: @ "share" style: Custom target: self action: @ selector (screenshotAction :)]; 26} 27 28-(void) screenshotAction :( id) sender {29 30 NSMutableArray * indexPaths = [NSMutableArray array]; 31 for (NSUInteger I = 0; I <[self. tableView numberOfRowsInSection: 0]; I ++) {32 [indexPaths addObject: [NSIndexPath indexPathForRow: I inSection: 0]; 33} 34 UIImage * image = [self screenShotForIndexPaths: indexPaths]; 35 UIActivityViewController * activityVC = [[UIActivityViewController alloc] initWithActivityItems: @ [image] applicationActivities: Nil]; 36 [self. navigationController presentViewController: activityVC animated: YES completion: NULL]; 37} 38 39 # pragma mark-UITableViewDataSource40-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {41 return kTableViewRowCount; 42} 43 44-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {45 return 1; 46} 47 48-(UITableViewCell *) tableView :( UITab LeView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {49 static NSString * mainCellId = @ "CellId"; 50 MainCell * mainCell = [tableView dequeueReusableCellWithIdentifier: mainCellId]; 51 if (! MainCell) {52 mainCell = [MainCell loadFromXib]; 53} 54 mainCell. accountNameLabel. text = [NSString stringWithFormat: @ "row: % ld", (long) indexPath. row]; 55 return mainCell; 56} 57 58 # pragma mark-UITableViewDelegate59-(void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {60 [tableView deselectRowAtIndexPath: indexPath animated: YES]; 61} 62-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath {63 return kTableViewHeight; 64} 65 66 # pragma mark-Screen shot Method67-(UIImage *) screenShotForIndexPaths :( NSArray *) indexPaths68 {69 CGPoint originalOffset = self. tableView. contentOffset; 70 71 uigraphicsbeginimagecontextwitexceptions (CGSizeMake (CGRectGetWidth (self. tableView. frame), self. tableView. rowHeight * indexPaths. count), NO, 0.0); 72 CGContextRef ctx = UIGraphicsGetCurrentContext (); 73 74 // render cells one by one on CGContext 75 MainCell * cell = nil; 76 for (NSIndexPath * indexPath in indexPaths) {77 78 // find the cell at the corresponding position and render it 79 [self. tableView scrollToRowAtIndexPath: indexPath atScrollPosition: UITableViewScrollPositionNone animated: NO]; 80 cell = (MainCell *) [self. tableView cellForRowAtIndexPath: indexPath]; 81 [cell. layer renderInContext: ctx]; 82 83 // in84 CGContextTranslateCTM (ctx, 0, self. tableView. rowHeight); 85} 86 87 UIImage * image = UIGraphicsGetImageFromCurrentImageContext (); 88 UIGraphicsEndImageContext (); 89 self. tableView. contentOffset = originalOffset; 90 return image; 91} 92 93 94-(void) didReceiveMemoryWarning {95 [super didreceivemorywarning]; 96 // Dispose of any resources that can be recreated.97} 98 99 @ end
TableView