Using PDFKit
in swift, you can use PDFDocument
to open pdf files.
That's easy and works well. But I'm building a custom pdf viewer (for comic book pdfs) that suits my needs and there is one problem I have. In a viewer, I don't need to have the whole pdf file in memory. I only need about a few pages at a time.
Also, the pdfs consist only of images. There's no text or anything.
When instantiating a PDFDocument
, the whole pdf data is being loaded into memory. If you have really huge pdf files (over 1GB) this isn't optimal (and can crash on some devices). As far as I know, there's no way in PDFKit to only load parts of a pdf document.
Is there anything I can do about that? I haven't found a swift/obj-c library that can do this (though I don't really know the right keywords to search for it).
My workaround would be to preprocess pdfs and save each page as image in the .documents director (or similar) using FileManager
. That would result in a tremendous amount of files but would solve the memory problem. I'm not sure I like this approach, though.
Update:
So I did what @Prcela and @Sahil Manchanda proposed. It seems to be working for now.
@yms: Hm, that could be a problem, indeed. Does this even happen when there are only images? Without anything else in the pdf.
@Carpsen90: They are local (saved in the documents directory).
EDIT: I haven't accepted the answer below, or given it the bounty. This was automatically. It does not solve the problem. It still loads the entire PDF into memory!