Advertisement
priore

Capture an image of a view not yet displayed

Jul 9th, 2015
1,256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Capture an image of a view, inside of a ViewController with Xib, not yet displayed.
  2. // use:
  3. // CaptureViewController *viewController = [[CaptureViewController alloc] initWithNibName:@"CaptureViewController" bundle:nil];
  4. // [viewController loadView];
  5. // UIImage *img = [viewController capture];
  6. //
  7. - (UIImage*)capture {
  8.    
  9.     UIGraphicsBeginImageContext(self.view.bounds.size);
  10.     CGContextRef context = UIGraphicsGetCurrentContext();
  11.    
  12.     [self.view.layer renderInContext:context];
  13.    
  14.     UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
  15.     UIGraphicsEndImageContext();
  16.    
  17.     return img;
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement