Search code examples
iosobjective-cuitableviewscreenshotuigraphicscontext

Taking a screenShot of view inside a cell in ios


I am trying to take a screen shot of view inside a cell of UITableView but with attached code I can able only to take a screenshot of cell bounds. The problem is that by UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f). I can only make screenshot of rect size and cannot control origin of rect and rect = [cell bounds]. so please suggest me some idea.

{
  UITableViewCell*  cell = [self.tableView cellForRowAtIndexPath:path];
  __block  CGRect rect = [cell bounds];
  UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);
  CGContextRef context = UIGraphicsGetCurrentContext();
  [cell.layer renderInContext:context];
  UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
}

Solution

  • Take the screenShot as a normal screenShot and then crop it

    -(UIImage *)cropImage:(UIImage *)image rect:(CGRect)cropRect
    {
       CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);
       UIImage *img = [UIImage imageWithCGImage:imageRef]; 
       CGImageRelease(imageRef);
       return img;
    }
    

    Use like this:

     UIImage *img = [self cropImage:viewImage rect:CGRectMake(150,150,100,100)];
    

    To get the frame of a particular tableView Cell. Use this:

    CGRect myRect = [tableView rectForRowAtIndexPath:0];//example 0,1,indexPath
    

    Hope this helps. Refer to this link link2