2

I have the following method to take a screenshot (UIImage) of a UIView which is far too slow

+ (UIImage *)imageWithView:(UIView *)view
{
  CGSize size = view.bounds.size;

  UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
  CGContextRef context = UIGraphicsGetCurrentContext();

  [view.layer renderInContext:context];
  UIImage * image = UIGraphicsGetImageFromCurrentImageContext();

  UIGraphicsEndImageContext()

  return image;
}

On my iPad I now have an app that needs this method to make a copy of a view that is drag&dropped. This view is one with rounded corners and therefore is not opaque (which not makes a difference to if I would set the isOpaque param to YES I found out)... Also the view that is screenshotted contains a UITableView with quite some complex entries in it...

Do you have any suggestions on how I can improve the speed of the screenshotting. Right now, for a bit bigger tableview (maybe 20 entries) it takes about 1 second (!!!) And the view is already on screen, rendered correctly... so I just need the Pixels to but into an UIImageView...

I need to support iOS 6+.

Greg
  • 9,068
  • 6
  • 49
  • 91
Georg
  • 3,664
  • 3
  • 34
  • 75

1 Answers1

2

I use this same code to take a screenshot of a really complex views. I think your bottleneck is using a big image for the drag&drop. Maybe you can resize the UIImage.

In my case the performance in a iPad2 is about 100ms for screenshot.

Jordi Coscolla
  • 1,066
  • 6
  • 8
  • Hm, the screenshot is not that super big. Its about 320 x 500... I think i found my bottleneck... It seems to be that the view being screenshotted has a layer shadow and rounded corners. Once i removed that, it's now much faster... I think I'll go with applying those properties to the UIImages' ImageView resulting out of the screenshot afterwards and I should be fine. Thanks for the answer though... – Georg Aug 04 '13 at 18:08