Question

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.

Was it helpful?

Solution

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.

  1. Find the CGRect of the UITableViewCell being clicked.
  2. Get the CGSzie from the CGRect of 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

enter image description here

Loaded the image view onto new view controller

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top