Thanks
Thanks

Reputation: 40339

Is it possible to get a rendered image out of the contents from an UIView?

I need a kind of "screenshot" from everything that's displayed in an UIView. Maybe there's a way to just access the image data that the renderer generated to display the contents on screen?

Upvotes: 0

Views: 143

Answers (1)

Louis Gerbarg
Louis Gerbarg

Reputation: 43452

The following should work. Depending on what sort of transforms you have defined on the view and layer you might need to apply some sort of rotation.

- (UIImage *)imageForView:(UIView *)view {
  UIGraphicsBeginImageContext(view.frame);
  [view.layer renderInContext: UIGraphicsGetCurrentContext()];
  UIImage *retval = UIGraphicsGetImageFromCurrentImageContext(void);
  UIGraphicsEndImageContext();

  return retval;
}

Upvotes: 3

Related Questions