mindbrix / UIImage-PDF

Simple UIImage PDF renderer category for iOS scalable assets
http://blog.mindbrix.co.uk/2012/02/10/ios-scalable-assets/
1.24k stars 155 forks source link

Image scaling and rotation issues. #37

Closed scottrhoyt closed 9 years ago

scottrhoyt commented 9 years ago

Thank you for the great category! I am using it to display pdf document pages as images in my app.

Firstly, I encountered an issue where some images are being rendered with a different than intended rotation. I believe I tracked this down to the /Rotate tag in the pdf not being honored while rendering. I came across the function, CGPDFPageGetDrawingTransform, that I used to replace the scaling and translating transforms in PDFView.m. This seems to have solved the problem.

Secondly, the resulting images that are being rendered are of low quality when displayed. They are of the intended size (taking into account screen scale), but perhaps the screen scale is not being taken into account somewhere? I noticed there was no reference to scale in the PDFView.m functions (including implied in the size parameter)... though you are passing in a context that has been scaled by the screen scale factor. Is this enough to ensure the PDFs are rendered at the proper size? Any other thoughts on what may be the issue?

skela commented 9 years ago

I too am having issues with the orientation of the image made from a PDF using this library. @scottrhoyt , where did you put the modifications to correct this ?

mindbrix commented 9 years ago

@scottrhoyt thanks for raising this. Can you submit your fix as a pull request? I'd like to see your code ;-)

scottrhoyt commented 9 years ago

I submitted a pull request with the code changes I made for orientation/scale. Didn't test extensively, but it solved the issues for me.

mindbrix commented 9 years ago

Thanks. I shall test it. Can you send me some examples PDFs where the rotation and scaling is wrong?

scottrhoyt commented 9 years ago

I'm done with the project that I was working on when I came across this, so I don't have access to the original files. However, I believe I identified the problem as stemming from when PDFs are rotated using the \Rotate tag in the PDF instead of being saved with new dimensions/orientation. This is most often seen not with a PDF vector image workflow, but instead with PDF documents where one or more pages in the document was rotated using this tag. We came across this when performing rotations in Acrobat. The transform function I used takes into account the \Rotate tag. I created a sample image that I believe should replicate this behavior here:

https://dl.dropboxusercontent.com/u/18408411/QuestionMark.pdf

I needed to remember to clean the project in between tests because of the default caching to disk.

mindbrix commented 9 years ago

Issue fixed by #38

skela commented 9 years ago

I tested it and it does indeed sort things out for the rotation, but I think the width and the height are the wrong way around. At least when I try it with my PDFs it seems to be the case. Same goes for the question mark. Its wider than the PDF suggests, and the only thing I can think of is that its got the dimensions the wrong way around.

mindbrix commented 9 years ago

Hi. I think that the last parameter of CGPDFPageGetDrawingTransform() in renderIntoContext: should be set to YES.

Can you see if that works?

mindbrix commented 9 years ago

@skela thanks for this.

mindbrix commented 9 years ago

ios simulator screen shot 20 may 2015 12 15 57

skela commented 9 years ago

@mindbrix np, i love yer work, happy to try and help ;)

Anyway, I tried setting the last parameter to YES for renderInContext as you suggested, and yes that did fix that issue, but introduced another one.

Basically now the Image has large areas of unused space, meaning its probably still got the dimensions the wrong way around. My gut feeling is there is probably a better fix than that.

mindbrix commented 9 years ago

@skela what PDF are you using to test?

skela commented 9 years ago

@mindbrix can I send it to you privately? Its a file provided by a client, don't want everybody to have it.

mindbrix commented 9 years ago

nigel@mindbrix.co.uk

skela commented 9 years ago

@mindbrix thanks mate, will send it to you now.

skela commented 9 years ago

@mindbrix ok, should be in your inbox any second now.

mindbrix commented 9 years ago

@skela I have created a branch at https://github.com/mindbrix/UIImage-PDF/tree/fixTransforms

This should fix your issues. Can you please test and let me know.

skela commented 9 years ago

@mindbrix I've tried the changes in that branch, its still got quite a bit of an empty space caused by the dimensions being the wrong way around. I'll send you the comparative results in an email so you can see what I mean.

mindbrix commented 9 years ago

@skela interesting, as I now have code to handle that.

skela commented 9 years ago

@mindbrix , for the record I'm using your [UIImage originalSizeImageWithPDFURL:(NSURL*)URL] ? category method.

just sent the email, should be with you any second ;)

mindbrix commented 9 years ago

@skela I've merged the changes. Thanks for your invaluable help ;-)

skela commented 9 years ago

@mindbrix awesome, cheers!