Reduce pdf size - Goal c

I have a PDF generation project, it consists of some texts and an image that is already stored in db, I want to view and send the created pdf file, everything is fine when there is only text data.

A problem arises if there is an image in our data. The email receives a pdf of 10 MB or higher, even if it has an image of 1 MB or lower. It works great in a simulator. My code for drawing an image is shown below:

UIImage *plotImage=[[UIImage alloc]initWithContentsOfFile:[localPictureArray objectAtIndex:i]]; CGSize imageSize=plotImage.size; CGFloat scaleFactor = 1.0; if (imageSize.height>(maxHeight-currentPageY) || imageSize.width>maxWidth ) { UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, kDefaultPageWidth, kDefaultPageHeight), nil); currentPageY = kMargin; if (!((scaleFactor = (maxWidth / imageSize.width)) > ((maxHeight-currentPageY) / imageSize.height))) { scaleFactor = (maxHeight-currentPageY) /imageSize.height; // scale to fit heigth. } CGRect rect = CGRectMake(kMargin,currentPageY, imageSize.width * scaleFactor, imageSize.height * scaleFactor); //Draw the image into the rect [plotImage drawInRect:rect]; } else { plotImage drawInRect:normal size)];//Normal size we need NSLog(@"%@",NSStringFromCGSize(plotImage.size)); } 

Since iam starter iam is trying to solve it.

+3
source share
2 answers

I fought a lot ... finally, when I wrote the image in jpeg format on the pdf page, using the code below, the size decreased ten times! I don’t know what is the right method ...

 UIImage *lowResImage = [UIImage imageWithData:UIImageJPEGRepresentation(plotImage, 0.02)]; 
+4
source

Instead of resizing the rectangle in context, you need to change the CTM (current transformation matrix) .

 CGContextSaveGState(theContext); CGContextScaleCTM(theContext, scaleFactor, scaleFactor); 

... drawing commands are here ...

 CGContextRestoreGState(theContext); 

( http://macdevcenter.com/pub/a/mac/2004/11/02/quartz.html )

0
source

All Articles