我的应用程序使用以下代码在 tvOS 应用程序中显示 PDF:
CGPDFDocumentRef pdfDocument2 = [self openPDF:[NSURL URLWithString:@"https://pdfislistedhere"]];
[self drawDocument:pdfDocument2];
但是,它似乎尝试绘制得太快了。有没有办法能够将完成处理程序添加到下面列出的 CGPDFDocumentRef 代码中?这是我得到帮助的代码,无法弄清楚是否有一个好的方法让它等待运行 drawDocument:pdfDocument 直到实际下载 PDF 之后?截至目前,它什么都不做,但会像成功一样运行所有检查和日志。
- (CGPDFDocumentRef)openPDF:(NSURL*)NSUrl {
CFURLRef url = (CFURLRef)CFBridgingRetain(NSUrl);
CGPDFDocumentRef myDocument;
myDocument = CGPDFDocumentCreateWithURL(url);
if (myDocument == NULL) {
NSLog(@"can't open %@", NSUrl);
CFRelease (url);
return nil;
}
CFRelease (url);
if (CGPDFDocumentGetNumberOfPages(myDocument) == 0) {
CGPDFDocumentRelease(myDocument);
return nil;
}
return myDocument;
}
- (void)drawDocument:(CGPDFDocumentRef)pdfDocument
{ NSLog(@"draw document");
theImageView.hidden = YES;
// Get the total number of pages for the whole PDF document
int totalPages= (int)CGPDFDocumentGetNumberOfPages(pdfDocument);
if ([[self.arrayOfVerses firstObject] isEqualToString:@"allverses"]) {
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 {
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];
}
}