In my TV app, I am able to load a PDF, draw it in a scroll view, and when the down button is pressed, it navigates to the next page. What I'd like to do, is be able to skip pages. This will correspond with a database where it loads a PDF, and the DB contains an NSArray with all the pages to skip i.e. [5,6,7,8,13,14,15,16]. When it draws the PDF, I only need it then to draw pages 1-4, and 9-12. What would be an elegant Obj-C way to handle this?
UPDATE: This first section of code is what I've added in the drawDocument section. It will remove, but it only removes whatever the last index from the array, in this case, the 8th page of the PDF, 7th in index. I want it to remove the 5th-8th pages, which corresponds to 4,5,6,7 from the Array when dealing with indexes. However, if I put the removeObjectsAtIndexes INSIDE the for loop, it removes, but because it's removing WHILE looping, it is skipping slides. If I do it outside the loop, it only removes the last index from the array
NSArray *testArray = @[@"4", @"5", @"6", @"7"];
for (NSString *object in testArray) {
//NSString *firstNumber = [testArray objectAtIndex:object];
NSInteger valueOfFirstNumber = [object intValue];
NSLog(@"Indexes%ld", (long)valueOfFirstNumber);
indexes = [[NSMutableIndexSet alloc] init];
[indexes addIndex:valueOfFirstNumber];
NSLog(@"%@", indexes);
}
[pageImages removeObjectsAtIndexes:indexes];
My current code for displaying a PDF in its entirety is:
- (void)drawDocument:(CGPDFDocumentRef)pdfDocument
{
// Get the total number of pages for the whole PDF document
int totalPages= (int)CGPDFDocumentGetNumberOfPages(pdfDocument);
self.pages = totalPages;
NSMutableArray *pageImages = [[NSMutableArray alloc] init];
// Iterate through the pages and add each page image to an array
for (int i = 1; i <= totalPages; i++) {
// Get the first page of the PDF document
CGPDFPageRef page = CGPDFDocumentGetPage(pdfDocument, i);
CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
// Begin the image context with the page size
// Also get the grapgics context that we will draw to
UIGraphicsBeginImageContext(pageRect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
// Rotate the page, so it displays correctly
CGContextTranslateCTM(context, 0.0, pageRect.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(page, kCGPDFMediaBox, pageRect, 0, true));
// Draw to the graphics context
CGContextDrawPDFPage(context, page);
// Get an image of the graphics context
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[pageImages addObject:image];
}
// Set the image of the PDF to the current view
[self addImagesToScrollView:pageImages];
}
-(void)addImagesToScrollView:(NSMutableArray*)imageArray {
int heigth = 0;
for (UIImage *image in imageArray) {
UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
imgView.contentMode = UIViewContentModeScaleAspectFit;
imgView.frame=CGRectMake(0, heigth-60, 1920, 1080);
[_scrollView addSubview:imgView];
heigth += imgView.frame.size.height;
}
}
-(void)viewDidLayoutSubviews {
NSLog(@"%ld", (long)self.pages);
_scrollView.contentSize = CGSizeMake(1920, 1080*self.pages);
}
-(void)nextSlide:(UITapGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
NSInteger totalHeight = ((self.pages - 1) * 1080) - 60;
NSLog(@"HEIGHT %ld", (long)totalHeight);
int theY = _scrollView.contentOffset.y;
int page = _scrollView.contentOffset.x / _scrollView.frame.size.width;
int nextPage = theY + 1080;
NSLog(@"page%d and nextpage%d", theY, nextPage);
if (theY >= totalHeight) {
NSLog (@"More Than");
}
else {
_scrollView.contentOffset = CGPointMake(0, nextPage);
}
}
}
-(void)previousSlide:(UITapGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
int theY = _scrollView.contentOffset.y;
int page = _scrollView.contentOffset.x / _scrollView.frame.size.width;
NSLog(@"Current %d", theY);
if (theY < 100) {
}
else {
int nextPage = theY - 1080;
NSLog(@"page%d and nextpage%d", page, nextPage);
_scrollView.contentOffset = CGPointMake(0, nextPage);
}
}
}