2013-03-05 67 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