PDF上的图像绘制无法通过转换绘制图像并输出错误

时间:2010-01-22 10:47:16

标签: iphone pdf uiwebview quartz-graphics cgpdfdocument

我在PDF上绘制图像时遇到问题。我会对图像应用缩放,旋转,移动等,并在PDF上绘制该图像。但绘图输出不正确。当我旋转图像时,它只是缩放而不会旋转。

在此,我将详细解释: 我在UIWebView上放置一个图像,使图像的伪效果完全在PDF上。然后,我在制作PDF时在UIWebView上绘制PDF图像。

但是当我使用已经应用了大量变换的修改后的图像准备PDF时,图像比例不会旋转。

这里,img_copyright是一个继承UIImageView的Custom类。

CGFloat orgY = (newY-whiteSpace)+img_copyright.frame.size.height;
CGFloat modY = pageRect.size.height-orgY;

CGFloat orgX = img_copyright.frame.origin.x-PDF_PAGE_PADDING_WIDTH;
CGFloat modX = orgX;

//We're getting X,Y of the image here to decide where to put the image on PDF.


CGRect drawRect = CGRectMake (modX, modY, img_copyright.frame.size.width, img_copyright.frame.size.height);

//Preparing the rectangle in which image is to be drawn.


CGContextDrawImage(pdfContext, drawRect, [img_copyright.image CGImage]);
    [img_copyright release];

// Actually drawing the image.

但是,当我看到PDF图像没有正确绘制到它中时。

你会建议,是因为基于X,Y的图像绘制引起的问题? 如果我们不依赖X,Y,我们如何决定将图像放在PDF上? 使用旋转,缩放比例在PDF上绘制图像的确切方法是什么?

当我将图像插入UIWebView的滚动条时的图像。 http://www.freeimagehosting.net/“>

当我将图像绘制到PDF上时的图像 http://www.freeimagehosting.net/“>

1 个答案:

答案 0 :(得分:1)

CGFloat angleInRadians =-1*Angle* (M_PI / 180.0);
CGAffineTransform transform=CGAffineTransformIdentity;

transform = CGAffineTransformMakeRotation(angleInRadians);
//transform = CGAffineTransformMakeScale(1, -1);
//transform =CGAffineTransformMakeTranslation(0,80);

CGRect rotatedRect = CGRectApplyAffineTransform(CGRectMake(0,0,Image.size.width,Image.size.height), transform);

 UIGraphicsBeginImageContext(rotatedRect.size);
 //[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
 CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0,rotatedRect.size.height);
 CGContextScaleCTM(UIGraphicsGetCurrentContext(),1, -1); 

 //CGContextTranslateCTM(UIGraphicsGetCurrentContext(), +(rotatedRect.size.width/2),+(rotatedRect.size.height/2));

 CGContextTranslateCTM(UIGraphicsGetCurrentContext(), (rotatedRect.origin.x)*-1,(rotatedRect.origin.y)*-1);
 CGContextRotateCTM(UIGraphicsGetCurrentContext(), angleInRadians);
 //CGContextTranslateCTM(UIGraphicsGetCurrentContext(), -(rotatedRect.size.width/2),-(rotatedRect.size.height/2));

 CGImageRef temp = [Image CGImage];

 CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, Image.size.width,Image.size.height), temp);

 //CGContextRotateCTM(UIGraphicsGetCurrentContext(), -angleInRadians);
 UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();

 return viewImage;