确切地说,在我发现的示例中,他使用了 CATiledLayer。
这是我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
CFURLRef pdfURL = CFBundleCopyResourceURL(
CFBundleGetMainBundle(),
CFSTR(filename),
CFSTR("pdf"), NULL);
myDocumentRef = CGPDFDocumentCreateWithURL(pdfURL);
myPageRef = CGPDFDocumentGetPage(myDocumentRef, 1);
CGRect pageRect = CGRectMake(0, 0, 320, 480);
tiledLayer = [CATiledLayer layer];
tiledLayer.delegate = self;
tiledLayer.tileSize = CGSizeMake(1024.0, 1024.0);
tiledLayer.levelsOfDetail = 1000;
tiledLayer.levelsOfDetailBias = 1000;
tiledLayer.frame = pageRect;
myContentView = [[UIView alloc] initWithFrame:pageRect];
[myContentView.layer addSublayer:tiledLayer];
CGRect viewFrame = self.view.frame;
viewFrame.origin = CGPointZero;
scrollView = [[UIScrollView alloc] initWithFrame:viewFrame];
scrollView.delegate = self;
scrollView.contentSize = pageRect.size;
scrollView.maximumZoomScale = 100;
[scrollView addSubview:myContentView];
[self.view addSubview:scrollView];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return myContentView;
}
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0);
CGContextFillRect(ctx, CGContextGetClipBoundingBox(ctx));
CGContextTranslateCTM(ctx, 0.0, layer.bounds.size.height);
CGContextScaleCTM(ctx, 1.0, -1.0);
CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(myPageRef, kCGPDFCropBox, layer.bounds, 0, true));
CGContextDrawPDFPage(ctx, myPageRef);
}
这部分效果很好,但是如果我想引入横向模式,我需要修改这部分:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if( (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) ||
(interfaceOrientation == UIInterfaceOrientationLandscapeRight)){
//SOMETHING TO DO FOR LANDSCAPE MODE
}
else if((interfaceOrientation == UIInterfaceOrientationPortrait)){
//SOMETHING TO DO TO RESTORE TO PORTRAIT
}
// Return YES for supported orientations
return ((interfaceOrientation == UIInterfaceOrientationPortrait) ||
(interfaceOrientation == UIInterfaceOrientationLandscapeLeft) ||
(interfaceOrientation == UIInterfaceOrientationLandscapeRight));
}
我几乎尝试了一切,但我找不到一个好的解决方案!