renderincontext stopped working in xcode 8

I've been working on an app where I have a UIView that needs to be printed via AirPrint. To accomplish this I first fill out all the details in the view and then pass it to the below code to make a pdf out of before printing it. In Xcode 7 everything was working fine. Since I did the update to Xcode 8, the view doesn't render to the correct size any more using renderInContext.


- (NSMutableData *)createPDFDataFromUIView:(UIView *)aView {
NSMutableData *pdfDataObject=[NSMutableData data];

UIGraphicsBeginPDFContextToData(pdfDataObject, [aView bounds], nil);

UIGraphicsBeginPDFPage();

CGContextRef pdfContext= UIGraphicsGetCurrentContext();

[[aView layer] renderInContext:pdfContext];

UIGraphicsEndPDFContext();

return pdfDataObject;
}


Any help would be appreciated.

What kind of device are you using? I have similar code that is still working on all my devices, but a user with an iPad Pro 9.7" with iOS 10 says it renders only a solid gray color.

Hi ckuipers

Do you see the same behavior when using -drawViewHierarchyInRect:afterScreenUpdates: ?

I think my problem is slightly different than the original poster, because he mentioned "render to the correct size" while in my case it only renders a solid gray image, and my issue only occurs on iPad Pro devices. But the rendering does work for me if I use drawViewHierarchyInRect instead of renderInContext. Unfortunately that's not a drop-in replacement because it causes other issues in my situation, so I just filed bug report 28804475. Until the previous functionality is restored, I am checking the device model identifier and using drawViewHierarchyInRect on iPad6,x devices and my original renderInContext code on other devices.

Same problem, on iPhone 7, iOS 10.1.1. Did you find any solutions? Thanks

No, it looks like renderInContext always renders gray on iPhone 7 and iPad Pro running iOS 10. Apple hasn't responded to my bug report, and the problem still exists in iOS 10.2. I gave up and reworked my app to use drawViewHierarchyInRect.


The problem with that, by the way, is that I'm capturing the contents of a UIWebView, and UIWebViewDelegate's webViewDidFinishLoad method is called before the web view is finished rendering. (I filed that as bug 10259756 years ago, but Apple closed it as "behaves correctly.") My workaround is to add a 1-second delay in webViewDidFinishLoad before running drawViewHierarchyInRect.

Hello,


Did you manage to resolve this issue ? I am having the same.

I found a workaround, I am not generating the PDF directly from the view.
I generate an image first from the View. Using drawViewHierarchy, renderInContext has the same effect (gray solid color). I save the generated image and load it in a UIWebview with HTML (loadHtmlString). Than I create the PDF from the UIWebview, this way I do not use renderInContext or drawWithHierarchy but UIPrintPageRenderer.

Now, the PDF is correctly generated and I can load it in a Webview, but when I send it by email or try to load it in iBooks the file is corrupted...

Well. I tried another strategy and this one works. I have my PDF and it's viewable in iBooks. Yay !

I convert the Scrollview to an image and I draw that image in a PDF file.


I am using Xamarin. if this can help anyone, here the code I used


public static string ToPDF (this UIScrollView view, string filename)
{
var image = view.ToUIImage ();
var bounds = new CGRect (0, 0, view.ContentSize.Width, view.ContentSize.Height);
var r = new UIGraphicsPdfRenderer (bounds, UIGraphicsPdfRendererFormat.DefaultFormat);
NSData pdf = r.CreatePdf ((UIGraphicsPdfRendererContext ctx) => {
ctx.BeginPage ();
image.Draw (new CGPoint (0, 0));
});
var filePath = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.Personal), filename);
pdf.Save (filePath, true);
return filePath;
}

public static UIImage ToUIImage (this UIScrollView view)
{
UIGraphics.BeginImageContext (view.ContentSize);
var context = UIGraphics.GetCurrentContext ();
var originFrame = view.Frame;
view.Frame = new CGRect (new CGPoint (0, 0), view.ContentSize);
view.Layer.RenderInContext (context);
view.Frame = originFrame;
var image = UIGraphics.GetImageFromCurrentImageContext ();
UIGraphics.EndImageContext ();
return image;
}

Hi,

I'm having the same exact problem as you. I found this post and solved it by simply change

[[aView layer] renderInContext:pdfContext];


to


[aView layer]draw(in:context) (* syntax might not be exactly correct since i'm not using objective-c)


Hope this helps others who have the same problem

renderincontext stopped working in xcode 8
 
 
Q