问题
How to get imaged using CGContext same as the tableviewcell that I Tapped. I got the frame of the cell which I tapped. I had seen many examples of CGContext it only takes the size as the input and converting the image into that size.
But I want to get the Image of the tableviewcell as it is with the same frame. Can anyone suggest me how can I do this.
回答1:
I guess You need to club these two logic in appropriate place after your cell is being tapped.
There is a method - (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath; to get the CGRect of your current cell.
- Find the
CGRectof theUITableViewCellbeing clicked. Get the
CGSziefrom theCGRectof step 1.-(UIImage *) screenshot:(CGRect )cellRect onCell:(UITableViewCell)cell { UIGraphicsBeginImageContext(cellRect.size); CGContextRef context=UIGraphicsGetCurrentContext(); [cell.layer renderInContext:context]; UIImage *image=UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; }
This should solve your problem, may need a few tweaks.
Here is usage:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIImageView *imageV = [[UIImageView alloc]initWithImage:[self screenshot:[tableView rectForRowAtIndexPath:indexPath] onCell:[tableView cellForRowAtIndexPath:indexPath]]];
imageV.frame = CGRectMake(0, 75, self.tableView.frame.size.width,100);
UIViewController *viewController = [[UIViewController alloc]init];
[viewController.view addSubview:imageV];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:viewController];
[self presentViewController:nav animated:YES completion:^{
}];
}
Screens : Selected row 1
Loaded the image view onto new view controller
来源:https://stackoverflow.com/questions/22904910/how-to-get-image-using-cgcontext-from-the-tableviewcell-when-tapped-on-the-ce