2013-03-05 47 views
3

我有一个pdf生成项目,它包含一些文本和一个已经存储在数据库中的图像,我想预览并发送pdf生成的,只有文本数据时一切正常。缩小pdf大小 - 目标c

如果我们的数据中存在图像,则会出现问题。邮件接收 大小为10MB的PDF或以上,即使它具有大小为1MB图像或below.It在simulator.My代码来绘制图像工作正常显示如下:

 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)); 
     } 

由于IAM起动机IAM努力解决它。

+0

仍然没有解决..任何人都可以帮助我! – 2013-03-06 08:06:21

+0

你的其他人在它前面没有'}',是只有一个错字吗? – Manuel 2013-03-06 08:38:18

+0

不,前面的else前面还没有关闭if:if(imageSize.height>(maxHeight-currentPageY)|| imageSize.width> maxWidth){'。不应该'else {'像这样:'} else {'? – Manuel 2013-03-06 08:47:45

回答

4

我挣扎了很多..最后在使用下面的代码以JPEG格式写图像PDF页面,尺寸减少了十倍!不知道这是正确的方法...

​​
0

而不是调整上下文中的矩形,您需要更改CTM(当前转换矩阵)

CGContextSaveGState(theContext); 


CGContextScaleCTM(theContext, scaleFactor, scaleFactor); 

...这里绘图命令......

CGContextRestoreGState(theContext); 

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

+0

谢谢,但CTM? – 2013-03-05 12:26:22

+0

石英以状态机的方式绘制对象。通过重新缩放矩形,您正在强制它在绘制之前重新插值图像,并因此添加额外的数据。 – kineticfocus 2013-03-05 12:31:13

+0

任何其他示例学习! – 2013-03-05 12:37:38