在 ios 的单元格内截屏视图

Taking a screenShot of view inside a cell in ios

我正在尝试对 UITableView 的单元格内的视图进行屏幕截图,但是使用附加代码我只能对单元格边界进行屏幕截图。问题是UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f)。我只能制作矩形大小的屏幕截图而无法控制矩形的原点和 rect = [cell bounds]。所以请给我一些想法。

{
  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();
}

将截屏当成普通截屏,然后裁剪

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

这样使用:

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

获取特定 tableView Cell 的框架。使用这个:

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

希望这对您有所帮助。参考这个link link2

- (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;

}