PDF и масштабирование - CATiledLayer - PullRequest
1 голос
/ 29 июня 2011

У меня проблема, я не программист, но я пытаюсь сделать какое-то приложение для развлечения.Я пытаюсь создать просмотрщик PDF, я создаю класс UIView следующим образом:

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:(CGRect)frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        NSString *pathToPdfDoc = [[NSBundle mainBundle] pathForResource:@"myPDF" ofType:@"pdf"];
        NSURL *pdfUrl = [NSURL fileURLWithPath:pathToPdfDoc];
        document = CGPDFDocumentCreateWithURL((CFURLRef)pdfUrl);
        currentPage = 1;
    }
    return self;
}

-(void)drawRect:(CGRect)inRect{                 
    if(document) {
        CGPDFPageRef page = CGPDFDocumentGetPage(document, currentPage);
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGContextSaveGState(ctx);
        CGContextTranslateCTM(ctx, 0.0, [self bounds].size.height);
        CGContextScaleCTM(ctx, 1.0, -1.0);
        CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, [self bounds], 0, true));
        CGContextDrawPDFPage(ctx, page);    
        CGContextRestoreGState(ctx);
    }
}

-(void)increasePageNumber {
    size_t pageCount = CGPDFDocumentGetNumberOfPages(document);
    if (currentPage == pageCount) {
        // do nothing
    }
    else {
        currentPage++;
        [self setNeedsDisplay];
    }
}

-(void)decreasePageNumber {
    if (currentPage == 1) {
        // do nothing
    }
    else {
        currentPage--;
        [self setNeedsDisplay];
    }
}

Теперь я пытаюсь добавить масштаб, но без удачи, для увеличения, я создаю слой:

-(void)viewDidLoad {
    CGRect frame;

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        frame = CGRectMake(0, 0, 960, 1024);

    }
    else{
        frame = CGRectMake(0, 0, 320, 460);

    }

    tiledLayer = [CATiledLayer layer];
    tiledLayer.delegate = self;
    tiledLayer.tileSize = CGSizeMake(1024, 1024);
    tiledLayer.levelsOfDetail = 200;
    tiledLayer.levelsOfDetailBias = 200;
    tiledLayer.frame = frame;

    myPDFView = [[viewPDF alloc] initWithFrame:frame];   
    //[self.view addSubview:myPDFView];  

    [myPDFView.layer addSublayer:tiledLayer];

    CGRect viewFrame = self.view.frame;
    viewFrame.origin = CGPointZero;
    scrollView = [[UIScrollView alloc] initWithFrame:viewFrame];
    scrollView.delegate = self;
    scrollView.contentSize = frame.size;
    scrollView.maximumZoomScale = 5;
    [scrollView addSubview:myPDFView];

    [self.view addSubview:scrollView];

}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return myPDFView;
}

РЕДАКТИРОВАТЬ: масштабирование работает, я использую shouldAutorotateToInterfaceOrientation, которая была проблемой (мне нужно переписать некоторый код), теперь она работает, НО, когда качество масштабирования ухудшается.(

...