Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use JPEG compression for images in pdf files generated on iOS?

Tags:

ios

pdf

iphone

ipad

Currently I am dealing with the problem that I have pdf file with lots of 1024x768 images in it and am trying to optimize the pdf's file size, but the only solution that I thought is good enough for now is compressing the images with jpeg compression. The problem is that I did not find any way to do that with iOS APIs. Am I missing something, is there a way? I`m welcome to suggestions on how to optimize the pdf with other means (lowering the resolution of the images is not a good solution for me).

Edit: If someone knows another API to use for pdf generation on iOS - links would be much appreciated.

Thanks in advance.

like image 374
worriorbg Avatar asked Apr 17 '12 08:04

worriorbg


2 Answers

Actually you can use a UIImageJPEGRepresentation. But there's another step, to then use a JPEG data provider to draw the image:

NSData *jpegData = UIImageJPEGRepresentation(sourceImage, 0.75);
CGDataProviderRef dp = CGDataProviderCreateWithCFData((__bridge CFDataRef)jpegData);
CGImageRef cgImage = CGImageCreateWithJPEGDataProvider(dp, NULL, true, kCGRenderingIntentDefault);
[[UIImage imageWithCGImage:cgImage] drawInRect:drawRect];

This worked well for me when drawing to a PDF context. 0.75 compression quality reduced the size of my image-laden PDF by about 75% and it still looked fine.

like image 81
Michael McNabb Avatar answered Nov 13 '22 06:11

Michael McNabb


If anyone is interested I found a solution to my problem by using a free pdf library - libHaru. It gave me the needed functionality to add a JPEG compressed image to the generated pdf file.

like image 33
worriorbg Avatar answered Nov 13 '22 04:11

worriorbg