简体   繁体   English

加载PDF时iOS滞后

[英]iOS Lag While Loading PDFs

So My Application uses, a lot of PDF's These sheet take a long time to load, (10 seconds + on iPad 2, iOS 7) So i figured if I could get the Load the separate sheets into the File directory, at ApplicationDidFinishLaunching() - I could then refers to the files in the user directory and they would load faster - but it made absolutely no difference 因此,“我的应用程序”使用了大量PDF文件。这些工作表需要很长时间才能加载(在iPad 2,iOS 7上为10秒以上),所以我想知道是否可以将单独的工作表加载到ApplicationDidFinishLaunching()的File目录中。 -然后,我可以引用用户目录中的文件,它们的加载速度会更快-但这绝对没有区别

Below is my DrawRect() method responsible for displaying the PDF 下面是我的DrawRect()方法,负责显示PDF

CGContextRef ctx = UIGraphicsGetCurrentContext();
//PDF might be transparent, assume white paper - set White Background
[[UIColor whiteColor] set];
CGContextFillRect(ctx, rect);

//PDF File Path
//"pdf" refers to the location in the directory where the file is saved"
CGPDFPageRef page1 = CGPDFDocumentGetPage(pdf, 1);

//Flip coordinates
CGContextGetCTM(ctx);
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -rect.size.height);

//Get the rectangle of the cropped inside
CGRect mediaRect = CGPDFPageGetBoxRect(page1, kCGPDFCropBox);

CGContextScaleCTM(ctx, rect.size.width / mediaRect.size.width,
                  rect.size.height / mediaRect.size.height);

//Draw PDF
CGContextDrawPDFPage(ctx, page1);
CGPDFDocumentRelease(pdf);

Location in the directory where the file is as was declared in the idInit() method 文件在idInit()方法中声明的目录中的位置

NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
    NSString *documentDirectory = [documentDirectories objectAtIndex:0];
    NSString *dataPath = [documentDirectory stringByAppendingPathComponent:
                          [NSString stringWithFormat:@"PDFSource"]];
    NSString *documentDirectoryFilename = [dataPath stringByAppendingPathComponent:
                                           [NSString stringWithFormat:@"BUEvenous.pdf"]];

    CFStringRef path = CFStringCreateWithCString (NULL, [documentDirectoryFilename UTF8String], kCFStringEncodingUTF8);
    CFURLRef url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, 0);
    CFRelease (path);
    pdf = CGPDFDocumentCreateWithURL(url);// 2
    CFRelease(url);

Your help guidance, suggestions, any points as to where I'm going wrong of how I could make this load faster is very much appreciated 非常感谢您的帮助指导,建议,有关我在哪里出问题的任何要点,例如如何使加载速度更快。

EDIT 编辑

@DavidvanDriessche thank you for the suggestion, it turnes out that the PDF files I am using are actually quite large (about 1 MB), what I learnt from testing is that if i further compress the PDF files so they're =< 500kbs then i don't have an issue with loading time - but compressing the files, lead to loss of image quality on some of the embedded images within the PDF : also it could be the iPad 2 - I'll try running it on a newer device and add to this post @DavidvanDriessche谢谢您的建议,事实证明我正在使用的PDF文件实际上很大(大约1 MB),我从测试中学到的是,如果我进一步压缩PDF文件,使它们= <500kbs,那么我的加载时间没有问题-但是压缩文件会导致PDF内的某些嵌入式图像的图像质量下降:也可能是iPad 2-我将尝试在较新的设备上运行它并添加到这篇文章

Any other suggestions ? 还有其他建议吗?

Task 任务

You could consider creating images from pdf pages instead, and do so in a background thread, via NSOperation or using dispatch_async 您可以考虑改为从pdf页面创建图像,然后通过NSOperation或使用dispatch_async在后台线程中创建图像

To create an image from a pdf page: 要从pdf页面创建图像:

+(UIImage *)getPDFPage:(CGPDFPageRef) page rect:(CGRect) rect
{

    CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);


    UIGraphicsBeginImageContext(pageRect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextTranslateCTM(context, 0.0, pageRect.size.height);

    CGContextScaleCTM(context, 1.0, -1.0);

    CGContextDrawPDFPage(context, page);

    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();
    return img;
}

EDIT: 编辑:

This code will give you an image in a size decided by the PDF file (the pageRect in code above). 此代码将为您提供由PDF文件( pageRect的代码中的pageRect )确定尺寸的图像。 Zoom level will therefore be decided by the media available. 因此,缩放级别将取决于可用的媒体。 Take a look at the different values for CGPDFPageGetBoxRect here . 此处查看CGPDFPageGetBoxRect的不同值。

Keep in mind that the images might take up a lot of memory. 请记住,图像可能会占用大量内存。 You could save the images to disk using: 您可以使用以下方法将图像保存到磁盘:

NSData *imageData = UIImagePNGRepresentation(pdfImage);

And then load the relevant page when needed. 然后在需要时加载相关页面。 Loading an image from the iUnit disk is pretty fast. 从iUnit磁盘加载映像非常快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM