我是IPhone软件开发工具包的新手。
我试着用CatiledLayer开发一个自定义的pdf阅读器,但我有很多问题。我不能加载pdf文档的所有页面,所以我尝试动态绘制页面,但此绘制函数的代码不起作用:
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0);
CGContextSaveGState(ctx);
for(int i=0; i<4;i++)
{
CGContextRestoreGState(ctx);
CGContextSaveGState(ctx);
CGContextFillRect(ctx, CGContextGetClipBoundingBox(ctx));
CGContextTranslateCTM(ctx, -layer.bounds.size.width*((5-2*i)/12), layer.bounds.size.height);
CGContextScaleCTM(ctx, 1.0, -1.0);
CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform([[pages objectAtIndex:i] pagin], kCGPDFCropBox, layer.bounds, 0, true));
CGContextDrawPDFPage(ctx, [[pages objectAtIndex:i] pagin]);
}
}此函数仅打印最后一页。
有什么想法吗?‘
魔术数字不正确,正确的代码是:
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
//scrollView.scrollEnabled=NO;
//scrollView.alpha=0.2;
//[scrollView setContentOffset:scrollView.contentOffset animated:YES];
CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0);
if(attua==1)
{
CGContextSaveGState(ctx);
CGContextFillRect(ctx, CGContextGetClipBoundingBox(ctx));
CGContextTranslateCTM(ctx,layer.bounds.size.width*1/(2*[pages count]) -layer.bounds.size.width*[pages count]/(2*[pages count]), layer.bounds.size.height);
CGContextScaleCTM(ctx, 1.0, -1.0);
CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform([[pages objectAtIndex:0] pagin], kCGPDFCropBox, layer.bounds, 0, true));
CGContextDrawPDFPage(ctx, [[pages objectAtIndex:0] pagin]);
CGContextRestoreGState(ctx);
}
for(int i=1; i<[pages count];i++)
{ //myContentView.frame=CGRectMake(0, 0,800*i, 1024);
if(i>attua-2)
{
if(i<attua+2)
{
CGContextSaveGState(ctx);
CGContextTranslateCTM(ctx,layer.bounds.size.width*(i*2+3/2)/(2*[pages count]) -layer.bounds.size.width*[pages count]/(2*[pages count]), layer.bounds.size.height);
CGContextScaleCTM(ctx, 1.0, -1.0);
CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform([[pages objectAtIndex:i] pagin], 0, layer.bounds, 0, true));
CGContextDrawPDFPage(ctx,[[pages objectAtIndex:i] pagin]);
CGContextRestoreGState(ctx);
}
}
}
scrollView.scrollEnabled=YES;
}现在一切正常了!!
发布于 2010-07-17 20:52:21
看起来你每次在for循环周围都要对整个上下文执行CGContextFillRect,从而清除了现有的材料(即除了最后一页之外的所有内容)。
我正在试着把我的头围绕在CATiledLayer绘图上。你似乎有一个转换坐标系的“魔术数字”;(5-2*i)/12。我想这是为了把页面排成一行。你确定这些数字是正确的吗?
https://stackoverflow.com/questions/3254809
复制相似问题