在ios中的一个单元格内拍摄一个屏幕

时间:2016-11-23 07:53:49

标签: ios objective-c uitableview screenshot uigraphicscontext

我正在尝试在UITableView的单元格内截取视图,但附加代码我只能截取单元格边界的截图。问题在于UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f)。我只能制作rect大小的截图,不能控制rect和的起源 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();
}

2 个答案:

答案 0 :(得分:5)

将screenShot作为普通screenShot拍摄,然后裁剪

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

答案 1 :(得分: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;

}