iphone某些PDF渲染为黑色图像
我正在尝试使用下面的代码绘制PDF页面。一些PDF的渲染正确,但另一些只是显示为完全黑色的图像,或者部分渲染,其余部分为黑色。在比较所发生的事情时,显示OK的似乎总是有“常规”文本以及一些图形(图表等),而显示为黑色的通常都是图形(例如,一页乐谱)。有人能给我指出正确的方向吗? 我在新的3.2SDK上构建了它 谢谢iphone某些PDF渲染为黑色图像,iphone,pdf,ipad,Iphone,Pdf,Ipad,我正在尝试使用下面的代码绘制PDF页面。一些PDF的渲染正确,但另一些只是显示为完全黑色的图像,或者部分渲染,其余部分为黑色。在比较所发生的事情时,显示OK的似乎总是有“常规”文本以及一些图形(图表等),而显示为黑色的通常都是图形(例如,一页乐谱)。有人能给我指出正确的方向吗? 我在新的3.2SDK上构建了它 谢谢 // PDF page drawing expects a Lower-Left coordinate system, so we flip the coordinate syste
// PDF page drawing expects a Lower-Left coordinate system, so we flip the coordinate system
// before we start drawing.
CGContextTranslateCTM(context, 0.0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// Grab the first PDF page
CGPDFPageRef page = CGPDFDocumentGetPage(myPDF, pageNo);
// We're about to modify the context CTM to draw the PDF page where we want it, so save the graphics state in case we want to do more drawing
CGContextSaveGState(context);
// CGPDFPageGetDrawingTransform provides an easy way to get the transform for a PDF page. It will scale down to fit, including any
// base rotations necessary to display the PDF page correctly.
CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, self.bounds, 0, true);
// And apply the transform.
CGContextConcatCTM(context, pdfTransform);
// Finally, we draw the page and restore the graphics state for further manipulations!
CGContextDrawPDFPage(context, page);
CGContextRestoreGState(context);
问题是背景颜色——具体来说,我遇到问题的pdf没有白色背景。通过用白色矩形填充视图,我的问题得到了解决。CGPDFDocumentRef MyGetPDFDocumentRef(const char*filename){ CFStringRef路径; cfurlRefURL; CGPDFDocumentRef文件; path=CFStringCreateWithCString(NULL,文件名,kCFStringEncodingUTF8); url=cfurlCreateWithFileSystemStyle(NULL,path,kCFURLPOSIXPathStyle,0); CFRelease(path); document=CGPDFDocumentCreateWithURL(url);//2 CFRelease(url); int count=CGPDFDocumentGetNumberOfPages(文档);//3 如果(计数=0){ printf(“`s'至少需要一页!”,文件名); 返回NULL; } 归还文件; }
void MyDisplayPDFPage(CGContextRef myContext,大小\u t页码,常量字符*文件名){
CGPDFDocumentRef文件;
CGPDFPageRef页面;
document=MyGetPDFDocumentRef(文件名);//1
page=CGPDFDocumentGetPage(文档,页码);//2
CGContextDrawPDFPage(myContext,第页);//3
cgContextTranslateCm(myContext,0,20);
CGContextScaleCTM(myContext,1.0,-1.0);
CGPDFDocumentRelease(document);//4
}
#导入“DashBoard.h”
#进口
@接口UIImage(WBImage)
//将UIImage旋转到任意角度
-(UIImage*)旋转:(UIImageOrientation)定向;
@结束
@实现UIImage(WBImage)
静态内嵌CGFloat度弧度(CGFloat度)
{
返回M_PI*(度/180.0);
}
静态内联CGSize swapWidthAndHeight(CGSize)
{
CGFloat swap=size.width;
size.width=size.height;
size.height=swap;
返回大小;
}
-(UIImage*)旋转:(UIImageOrientation)定向
{
CGRect bnds=CGRectZero;
UIImage*复制=无;
CGContextRef ctxt=nil;
CGRect rect=CGRectZero;
CGAffineTransform tran=CGAffineTransformity;
bnds.size=self.size;
rect.size=self.size;
开关(方向)
{
案例UIP:
回归自我;
案例UIImageOrientationUpErrored:
tran=CGAffineTransformMakeTransform(rect.size.width,0.0);
tran=CGAffineTransformScale(tran,-1.0,1.0);
打破
案例UIImageOnDown:
tran=CGAffineTransformMakeTransform(rect.size.width,
矩形尺寸、高度);
tran=CGAffineTransformRotate(tran,度弧度(180.0));
打破
案例UIImageOrientionDownMirrored:
tran=CGAffineTransformMakeTransform(0.0,rect.size.height);
tran=CGAffineTransformScale(tran,1.0,-1.0);
打破
案例UIImageOrientationLeft:
bnds.size=swapWidthAndHeight(bnds.size);
tran=CGAffineTransformMakeTransform(0.0,rect.size.width);
tran=CGAffineTransformRotate(tran,度弧度(-90.0));
打破
案例UIImageOrientionReftMirror:
bnds.size=swapWidthAndHeight(bnds.size);
tran=CGAffineTransformMakeTransform(rect.size.height,
矩形尺寸、宽度);
tran=CGAffineTransformScale(tran,-1.0,1.0);
tran=CGAffineTransformRotate(tran,度弧度(-90.0));
打破
案例UIImageOrientationRight:
bnds.size=swapWidthAndHeight(bnds.size);
tran=CGAffineTransformMakeTransform(rect.size.height,0.0);
tran=CGAffineTransformRotate(tran,度弧度(90.0));
打破
案例UIImageOrientionRightMirrored:
bnds.size=swapWidthAndHeight(bnds.size);
tran=CGAffineTransformMakeScale(-1.0,1.0);
tran=CGAffineTransformRotate(tran,度弧度(90.0));
打破
违约:
//提供的方向值无效
断言(假);
返回零;
}
UIGraphicsBeginImageContext(bnds.size);
ctxt=UIGraphicsGetCurrentContext();
开关(方向)
{
案例UIImageOrientationLeft:
案例UIImageOrientionReftMirror:
案例UIImageOrientationRight:
案例UIImageOrientionRightMirrored:
CGContextScaleCTM(ctxt,-1.0,1.0);
CGContextTranslateCTM(ctxt,-rect.size.height,0.0);
打破
违约:
CGContextScaleCTM(ctxt,1.0,-1.0);
CGContextTranslateCTM(ctxt,0.0,-rect.size.height);
打破
}
CGContextConcatCTM(ctxt,tran);
CGContextDrawImage(ctxt、rect、self.CGImage);
copy=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
返回副本;
}
@结束
@实现仪表板
//实现viewDidLoad以在加载视图(通常从nib)后执行附加设置。
-(无效)viewDidLoad{
[超级视图下载];
文件路径=[[NSString alloc]initWithString:[[NSBundle mainBundle]路径资源:@“pdf”类型的“roadie”;
//IbUIScrollView
[scrMain setContentSize:CGSizeMake(720720)];
//ibuiimageview
[imgV setFrame:CGRectMake(0,0,720,720)];
UIGraphicsBeginImageContext(CGSizeMake(720720));
//初始页面是一个大小变量
初始页=1;
void MyDisplayPDFPage (CGContextRef myContext,size_t pageNumber,const char *filename) {
CGPDFDocumentRef document;
CGPDFPageRef page;
document = MyGetPDFDocumentRef (filename);// 1
page = CGPDFDocumentGetPage (document, pageNumber);// 2
CGContextDrawPDFPage (myContext, page);// 3
CGContextTranslateCTM(myContext, 0, 20);
CGContextScaleCTM(myContext, 1.0, -1.0);
CGPDFDocumentRelease (document);// 4
}
#import "DashBoard.h"
#import <QuartzCore/QuartzCore.h>
@interface UIImage (WBImage)
// rotate UIImage to any angle
-(UIImage*)rotate:(UIImageOrientation)orient;
@end
@implementation UIImage (WBImage)
static inline CGFloat degreesToRadians(CGFloat degrees)
{
return M_PI * (degrees / 180.0);
}
static inline CGSize swapWidthAndHeight(CGSize size)
{
CGFloat swap = size.width;
size.width = size.height;
size.height = swap;
return size;
}
-(UIImage*)rotate:(UIImageOrientation)orient
{
CGRect bnds = CGRectZero;
UIImage* copy = nil;
CGContextRef ctxt = nil;
CGRect rect = CGRectZero;
CGAffineTransform tran = CGAffineTransformIdentity;
bnds.size = self.size;
rect.size = self.size;
switch (orient)
{
case UIImageOrientationUp:
return self;
case UIImageOrientationUpMirrored:
tran = CGAffineTransformMakeTranslation(rect.size.width, 0.0);
tran = CGAffineTransformScale(tran, -1.0, 1.0);
break;
case UIImageOrientationDown:
tran = CGAffineTransformMakeTranslation(rect.size.width,
rect.size.height);
tran = CGAffineTransformRotate(tran, degreesToRadians(180.0));
break;
case UIImageOrientationDownMirrored:
tran = CGAffineTransformMakeTranslation(0.0, rect.size.height);
tran = CGAffineTransformScale(tran, 1.0, -1.0);
break;
case UIImageOrientationLeft:
bnds.size = swapWidthAndHeight(bnds.size);
tran = CGAffineTransformMakeTranslation(0.0, rect.size.width);
tran = CGAffineTransformRotate(tran, degreesToRadians(-90.0));
break;
case UIImageOrientationLeftMirrored:
bnds.size = swapWidthAndHeight(bnds.size);
tran = CGAffineTransformMakeTranslation(rect.size.height,
rect.size.width);
tran = CGAffineTransformScale(tran, -1.0, 1.0);
tran = CGAffineTransformRotate(tran, degreesToRadians(-90.0));
break;
case UIImageOrientationRight:
bnds.size = swapWidthAndHeight(bnds.size);
tran = CGAffineTransformMakeTranslation(rect.size.height, 0.0);
tran = CGAffineTransformRotate(tran, degreesToRadians(90.0));
break;
case UIImageOrientationRightMirrored:
bnds.size = swapWidthAndHeight(bnds.size);
tran = CGAffineTransformMakeScale(-1.0, 1.0);
tran = CGAffineTransformRotate(tran, degreesToRadians(90.0));
break;
default:
// orientation value supplied is invalid
assert(false);
return nil;
}
UIGraphicsBeginImageContext(bnds.size);
ctxt = UIGraphicsGetCurrentContext();
switch (orient)
{
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
CGContextScaleCTM(ctxt, -1.0, 1.0);
CGContextTranslateCTM(ctxt, -rect.size.height, 0.0);
break;
default:
CGContextScaleCTM(ctxt, 1.0, -1.0);
CGContextTranslateCTM(ctxt, 0.0, -rect.size.height);
break;
}
CGContextConcatCTM(ctxt, tran);
CGContextDrawImage(ctxt, rect, self.CGImage);
copy = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return copy;
}
@end
@implementation DashBoard
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
filePath=[[NSString alloc] initWithString:[[NSBundle mainBundle] pathForResource:@"roadie" ofType:@"pdf"]];
// an IBOutlet UIScrollView
[scrMain setContentSize:CGSizeMake(720, 720)];
// an IBOutlet UIImageView
[imgV setFrame:CGRectMake(0, 0, 720, 720)];
UIGraphicsBeginImageContext(CGSizeMake(720, 720));
// initial page is an size_t variable
initialPage=1;
MyDisplayPDFPage(UIGraphicsGetCurrentContext(), initialPage, [filePath UTF8String],myTable,mainString);
imgV.image=UIGraphicsGetImageFromCurrentImageContext();
imgV.image=[imgV.image rotate:UIImageOrientationDownMirrored];
}