0

How can I take a screenshot of an iPad/iPhone App and limit it to "UIImageView A" bounds, but also have the screenshot include any controls (UIImageView's or UILabels) that happen to be positioned on top of "UIImageView A"? Will it mess up when standard display vs retina display?

THANKS IN ADVANCE!

example

dan
  • 393
  • 1
  • 14

2 Answers2

2

Another idea would be to simply take a screenshot the old fashioned way, e.g. see the answer in this related question and then crop it to the frame rect of your "A" UIView.

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
0

You'll need an enclosing view, because UIImageView isn't intended to have subviews. Put everything you want to capture inside that enclosing view (a simple UIView will do). The following is a very simple implementation that ignores things like view transformations:

// 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
}
Tony
  • 3,470
  • 1
  • 18
  • 23
  • wow this helped a lot.. Any idea why the image would be monochrome? – dan Mar 30 '13 at 15:24
  • No idea. I wrote a test app to try this code out before I posted it and it worked fine for me. What class view are you using in the takeScreenshot: method? – Tony Apr 01 '13 at 17:15
  • I am using self.view ? – dan Apr 04 '13 at 13:40