标签:des style blog http io color ar os sp
话不多说,直接上代码。
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 30 13 #define kTableViewHeight 76 14 15 @interface MainViewController () 16 @property (strong, nonatomic) IBOutlet UITableView *tableView; 17 @end 18 19 @implementation MainViewController 20 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:UIBarButtonItemStylePlain 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 - UITableViewDataSource 40 - (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:(UITableView *)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 - UITableViewDelegate 59 - (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 Method 67 - (UIImage*)screenShotForIndexPaths:(NSArray*)indexPaths 68 { 69 CGPoint originalOffset = self.tableView.contentOffset; 70 71 UIGraphicsBeginImageContextWithOptions(CGSizeMake(CGRectGetWidth(self.tableView.frame), self.tableView.rowHeight * indexPaths.count), NO, 0.0); 72 CGContextRef ctx = UIGraphicsGetCurrentContext(); 73 74 //将cell逐个渲染到CGContext上 75 MainCell *cell = nil; 76 for (NSIndexPath *indexPath in indexPaths) { 77 78 //找到相应位置的cell,渲染出來 79 [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:NO]; 80 cell = (MainCell *)[self.tableView cellForRowAtIndexPath:indexPath]; 81 [cell.layer renderInContext:ctx]; 82 83 //在context上渲染的origin 84 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 didReceiveMemoryWarning]; 96 // Dispose of any resources that can be recreated. 97 } 98 99 @end
标签:des style blog http io color ar os sp
原文地址:http://www.cnblogs.com/chenjungang/p/4082849.html