iOS SDK - 部分截图?

时间:2013-03-29 14:34:34

标签: iphone objective-c ipad

我如何拍摄iPad / iPhone应用程序的屏幕截图并将其限制为“UIImageView A”边界,但屏幕截图中还包含碰巧位于“UIImageView A”顶部的任何控件(UIImageView或UILabels) ?当标准显示与视网膜显示时,它会搞砸吗?

感谢提前!

example

2 个答案:

答案 0 :(得分:2)

另一个想法是简单地以老式的方式截取屏幕截图,例如see the answer in this related question然后将其裁剪到“A”UIView的框架矩形。

答案 1 :(得分:0)

您需要一个封闭视图,因为UIImageView不具有子视图。将您想要捕获的所有内容放在封闭视图中(简单的UIView会这样做)。以下是一个非常简单的实现,它忽略了视图转换等内容:

// Helper function, calls drawRect: recursively
void drawViewAndSubviews (UIView* view) {
  [view drawRect:view.bounds];
  CGContextRef ctx = UIGraphicsGetCurrentContext();
  for (UIView* subview in view.subviews) {
    CGPoint origin = subview.frame.origin;
    CGContextTranslateCTM(ctx, origin.x, origin.y);
    drawViewAndSubviews(subview);
    CGContextTranslateCTM(ctx, -origin.x, -origin.y);
  }
}

- (void)takeScreenshot:(UIView *)view {
  UIGraphicsBeginImageContext(view.bounds.size);
  drawViewAndSubviews(view);
  UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  // Now 'image' has a snapshot of 'view' and its subviews
}