I'm trying to draw content in a UIWebView
instead of a UIView
, because I like UIWebView
's ability to zoom in and out by pinching. Here's my code:
// setup environment
CGRect outputRect = myWebView.bounds;
CFMutableDataRef data = CFDataCreateMutable(NULL, 0); // init with default allocator and unlimited size
CGDataConsumerRef dataConsumer = CGDataConsumerCreateWithCFData(data);
CGContextRef pdfContext = CGPDFContextCreate(dataConsumer, &outputRect, NULL);
CGPDFContextBeginPage(pdfContext, NULL);
// draw something
CGContextSetRGBFillColor (pdfContext, 1, 0, 0, 1);
CGContextFillRect (pdfContext, CGRectMake (0, 0, 200, 100 ));
CGContextSetRGBFillColor (pdfContext, 0, 0, 1, .5);
CGContextFillRect (pdfContext, CGRectMake (0, 0, 100, 200 ));
CGPDFContextEndPage(pdfContext);
// load drawing in webView
[myWebView loadData:(NSData *)data MIMEType:@"application/pdf" textEncodingName:@"utf-8" baseURL:nil];
// cleanup
if(data != NULL) CFRelease(data); // always make sure to not pass NULL in CFRelease
CGDataConsumerRelease(dataConsumer);
CGContextRelease(pdfContext);
Behind the UIWebView
there other things going on which I want visible, so I have made the UIWebView
's background transparent like so:
myWebView.opaque = NO; //otherwise setting background color has no effect
myWebView.backgroundColor = [UIColor clearColor];
This works correctly if I don't load the pdf. However when the pdf loads, its page has a white background and a shadow, like in the screenshot below. This is messing up my UI design. How can I get rid of the shadow and make the pdf page transparent?
Thanks