1

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();
}
Bhavin Ramani
  • 3,221
  • 5
  • 30
  • 41
Saransh Gaur
  • 193
  • 2
  • 9
  • Just change `[cell bounds]` with the frame of the view inside that cell that you need to take image of? Have you tried that? – Tj3n Nov 23 '16 at 08:36
  • i think instead of [cell.layer renderInContext:context]; you should be doing [cell.DesiredView.layer renderInContext:context]; you want to put what you want in the screenshot in the context – yawnobleix Nov 23 '16 at 09:03

2 Answers2

5

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

Community
  • 1
  • 1
Md. Ibrahim Hassan
  • 5,359
  • 1
  • 25
  • 45
0
- (UIImage *) screenshot {

     CGSize size = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);

     UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);

     CGRect rec = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); //set the frame 
     [self.view drawViewHierarchyInRect:rec afterScreenUpdates:YES];

     image = UIGraphicsGetImageFromCurrentImageContext();
     UIGraphicsEndImageContext();
     return image;

}

Dishant Rajput
  • 1,329
  • 1
  • 10
  • 20