减少pdf大小 - 目标c

时间:2013-03-05 12:17:58

标签: ios objective-c uiimage quartz-2d

我有一个pdf生成项目,它包含一些文本和已存储在db中的图像,我想预览并邮寄生成的pdf,当只有文本数据时一切正常。

如果我们的数据中有图像,则会出现问题。邮件收到 大小为10MB或以上的pdf,即使它有1MB或更小的图像。它在模拟器中工作正常。我绘制图像的代码如下所示:

        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是一个努力解决它的初学者。

2 个答案:

答案 0 :(得分:4)

我挣扎了很多..最后用jpeg格式将图像写成pdf页面使用下面的代码,大小减少了十倍!!不知道这是正确的方法......

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

答案 1 :(得分:0)

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

CGContextSaveGState(theContext);


CGContextScaleCTM(theContext, scaleFactor, scaleFactor);

...在这里绘制命令......

CGContextRestoreGState(theContext);

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