5

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.

worriorbg
  • 351
  • 5
  • 14
  • there isn't any out of the box solution for optimizing a pdf with iOS apis, sry! – CarlJ Apr 17 '12 at 08:24
  • http://stackoverflow.com/questions/4362734/ios-sdk-programmatically-generate-a-pdf-file – Hanuman Apr 17 '12 at 08:25
  • http://blog.nitropdf.com/2008/02/5-tricks-to-shrinkreduce-pdf-file-size/ just to understand the ways you can reduce pdf file size. – Hanuman Apr 17 '12 at 08:28

3 Answers3

9

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.

fthiella
  • 48,073
  • 15
  • 90
  • 106
1

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.

worriorbg
  • 351
  • 5
  • 14
-1

If you'd like to compress images you can use UIImageJPEGRepresentation

NSData * UIImageJPEGRepresentation (
   UIImage *image,
   CGFloat compressionQuality
);

you could jpeg convert/compress a PNG image with

NSData *someImageData = UIImageJPEGRepresentation(pngImage, 0.6); // quality 0.6 (60%) (from 0.0-1.0).
UIImage *newCompressedJPG = [UIImage imageWithData:someImageData];

But i don't think so this will reduce your PDF size. Because when you place the UIImage to your pdf the RAW image get's placed (as far as i know).

Update 1: its compressibility varies from 0.0 to 1.0 (thanks to Leena)

Community
  • 1
  • 1
Jonas Schnelli
  • 9,965
  • 3
  • 48
  • 60
  • Thanks for the comment and you are right, when drawing the image into the pdf context the actual RAW image data is drawn. What I need is a way to add the jpeg compressed image - this could possibly need to use something other than quartz and CGPDF. – worriorbg Apr 18 '12 at 09:32