下载的 PDF 无法访问

问题描述 投票:0回答:1

我的 tvOS 应用程序设置的主要功能是绘制已嵌入应用程序本身的音乐表的 PDF。这里有多个选项,因为它可能只显示歌曲的某些章节。我为此使用的代码是:

- (void)drawDocument:(CGPDFDocumentRef)pdfDocument
{   NSLog(@"draw document");
    // Get the total number of pages for the whole PDF document
    int  totalPages= (int)CGPDFDocumentGetNumberOfPages(pdfDocument);

    if ([[self.arrayOfVerses firstObject] isEqualToString:@"allverses"]) {
        //This is if all verses have been picked and it will do the entire PDF
        NSLog(@"draw document all verses %d", totalPages);
        self.pages = totalPages;
        self.subtractingPages = totalPages - 1;

        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];
    }
    else {
        //Only a certain number of verses
        self.pages = [self.arrayOfVerses count];
        self.subtractingPages = self.pages - 1;

        NSLog (@"total pages %ld", (long)self.pages);
        NSMutableArray *pageImages = [[NSMutableArray alloc] init];
        
        
        [pageImages removeAllObjects];
        
        //Getting slide numbers to be used
        NSString *text = self.selectedCountry;
        NSString *substring = nil;
        
        NSRange parenRng = [text rangeOfString: @"(?<=\\().*?(?=\\))" options: NSRegularExpressionSearch];
        
        if ( parenRng.location != NSNotFound ) {
            substring = [text substringWithRange:parenRng];
            NSLog(@"Substring %@", substring);
        }
        
        
        // 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
            
            
            NSPredicate *valuePredicate=[NSPredicate predicateWithFormat:@"self.intValue == %d",i];
            
            if ([[self.arrayOfVerses filteredArrayUsingPredicate:valuePredicate] count]!=0) {
                // FOUND
                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];
            }
            
            else  {
                //NOT FOUND
            }
            
        }
        // 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 {
    
    _scrollView.contentSize = CGSizeMake(1920, 1080*self.pages);
    
}

然而,有一个部分需要下载 PDF,然后绘制。我使用 Back4app 作为它的主机,它的平均 PDF 是 420kb。我目前有它这样做:

- (void)sermonTime {
    if (theImageView.hidden == NO) {
        theImageView.hidden = YES;
    }
    if ([self.entry[@"SermonPresIncluded"] isEqualToString:@"NO"]) {
        //there are some instances where there is no PDF to download, following method just displays black screen
        [self blankTime];
    }
    else {
        NSLog(@"SermonTime");
        PFFileObject *thumbnail = self.entry[@"SermonPDF"];
        NSString *tempDir = NSTemporaryDirectory();
       
        NSString *pdfPath = [[tempDir stringByAppendingPathComponent:[self.entry valueForKey:@"DateOfService"]] stringByAppendingString:@".pdf"];


            [thumbnail getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
                if (error) {
                    NSLog(@"Error downloading PDF: %@", error);
                    [self blankTime];
                } else {
                    
                    [imageData writeToFile:pdfPath atomically:YES];
                        // Use completion block to signal that the PDF is ready to display
                       
                    sermonPDFURL = [NSURL fileURLWithPath:pdfPath];

                    [self performSelector:@selector(doitnowpdf) withObject:NULL afterDelay:5];
                   
                            self.view.backgroundColor = [UIColor blackColor];
                          
                      
                    
                }
            }];
        
        }
    
}
-(void)doitnowpdf {
    self.arrayOfVerses = @[@"allverses"];

    CGPDFDocumentRef pdfDocument = [self openPDF:sermonPDFURL];
    [self drawDocument:pdfDocument];
}

我遇到了一些问题,似乎问题可能出在它试图在下载完成之前绘制 PDF,这就是为什么我在那里延迟 5 秒。然而,它仍然会搞砸,即使日志显示它在绘制开始时知道 PDF 中只有一页,所以我知道它已完全下载。发生这种情况时,上一首歌曲的 PDF 会保留在屏幕上。如果我尝试执行代码以转到下一个 PDF,它会在应该下载的内容之后转到那个,如果我返回,它会正确绘制它。我迫切希望以任何方式正确完成这项工作。

objective-c pdf parse-platform tvos pfquery
1个回答
0
投票

您不应使用固定的 5 秒延迟,而应使用完成处理程序来确保绘图仅在文件完全下载后发生。

` - (void)sermonTime { 如果(theImageView.hidden == NO){ theImageView.hidden = YES; } 如果 ([self.entry[@"SermonPresIncluded"] isEqualToString:@"NO"]) { //有些情况下没有PDF可以下载,下面的方法只是黑屏 [自空白时间]; } 别的 { NSLog(@"布道时间"); PFFileObject *thumbnail = self.entry[@"SermonPDF"]; NSString *tempDir = NSTemporaryDirectory();

    NSString *pdfPath = [[tempDir stringByAppendingPathComponent:[self.entry valueForKey:@"DateOfService"]] stringByAppendingString:@".pdf"];

    [thumbnail getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
        if (error) {
            NSLog(@"Error downloading PDF: %@", error);
            [self blankTime];
        } else {
            [imageData writeToFile:pdfPath atomically:YES];
            sermonPDFURL = [NSURL fileURLWithPath:pdfPath];
            self.arrayOfVerses = @[@"allverses"];
            CGPDFDocumentRef pdfDocument = [self openPDF:sermonPDFURL];
            [self drawDocument:pdfDocument];
            self.view.backgroundColor = [UIColor blackColor];
        }
    }];
}

} `

© www.soinside.com 2019 - 2024. All rights reserved.