减小PDF文件大小 - Objective C

3

我有一个生成PDF的项目,其中包含一些文本和已在数据库中存储的图像,我想预览和发送生成的PDF,当只有文本数据时一切正常。

如果我们的数据中有图像,则会出现问题。即使图片大小为1MB或更小,邮件接收到的PDF文件大小也会达到10MB或以上。在模拟器中一切正常。下面是我的绘制图像代码:

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

由于我是一名初学者,所以我很难解决这个问题。


仍未解决..有人能帮我吗! - Mumthezir VP
你的 else 前面没有 },这只是一个打字错误吗? - Manuel
不,else 仍然没有一个 } 在它前面来关闭它前面的 if:if (imageSize.height>(maxHeight-currentPageY) || imageSize.width>maxWidth ) {。难道 else { 应该是这样的:} else { - Manuel
除了drawInRect之外,还有其他的方法吗? - Mumthezir VP
2个回答

4

我曾经苦苦挣扎... 最终,当我使用以下代码将jpeg格式的图像写入pdf页面时,大小减小了十倍!! 不知道这是否是正确的方法...

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

0

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

CGContextSaveGState(theContext);


CGContextScaleCTM(theContext, scaleFactor, scaleFactor);

在此处放置绘图命令...

CGContextRestoreGState(theContext);

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


Quartz以状态机方式绘制对象。通过重新缩放矩形,您强制它在绘制之前重新插值图像,因此添加了额外的数据。 - kineticfocus
苹果有很好的文档,或者可以参考Ray Wenderlich网站。 - kineticfocus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接