Note that there are some explanatory texts on larger screens.

plurals
  1. POUIGraphicsBeginImageContextWithOptions not capturing shadows / gradients
    primarykey
    data
    text
    <p><strong>THE PROBLEM</strong></p> <p>I'm using UIGraphicsBeginImageContextWithOptions to save a screenshot into a UIImage. While this works, the resulting UIImage is missing any shadows and gradients.</p> <p>How do I get it to also screenshot the layer stuff?</p> <hr> <p><strong>TEST CODE SHOWING THE PROBLEM</strong></p> <hr> <pre><code>#import "ViewController.h" #import &lt;QuartzCore/QuartzCore.h&gt; @implementation ViewController - (void)viewDidLoad { //super [super viewDidLoad]; //background self.view.backgroundColor = [UIColor whiteColor]; //container UIView *container = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 300, 200)]; container.backgroundColor = [UIColor blackColor]; [self.view addSubview:container]; //circle UIView *circle = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 10, 10)]; circle.backgroundColor = [UIColor whiteColor]; circle.layer.cornerRadius = 5; circle.layer.shadowColor = [UIColor redColor].CGColor; circle.layer.shadowOffset = CGSizeZero; circle.layer.shadowOpacity = 1; circle.layer.shadowRadius = 10; circle.layer.shadowPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-10, -10, 30, 30)].CGPath; [container addSubview:circle]; //screenshot UIGraphicsBeginImageContextWithOptions(container.bounds.size, container.opaque, 0.0); [container.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); //image view UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(10, 568-200-20-10, 300, 200)]; iv.image = screenshot; [self.view addSubview:iv]; } @end </code></pre> <hr> <p><strong>SIMULATOR SCREENSHOT SHOWING THE PROBLEM</strong></p> <hr> <p>The top area is the original view. The bottom area is a UIImageView with the screenshot.</p> <p><img src="https://i.stack.imgur.com/wHGLu.png" alt="Simulator Screenshot"></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload