Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct PdfFile {
- var filepath: String?
- var file: PDFDocument?
- private var cacheImages = NSCache<NSString, UIImage>()
- var numberOfPages: Int {
- return file?.pdfDocument?.numberOfPages ?? 0
- }
- init(filepath path: String?) {
- filepath = path
- file = PDFDocument.open(file: path)
- }
- mutating func page(forIndex index: Int, scaleFactor: CGFloat) -> UIImage? {
- guard let pdf = file else { return nil }
- let stringIndex = String(index) as NSString
- if let image = cacheImages.object(forKey: stringIndex) {
- return image
- } else {
- let page = index + 1
- let image = pdf.render(page: page, scaleFactor: scaleFactor)
- if let image = image {
- cacheImages.setObject(image, forKey: stringIndex)
- }
- return image
- }
- }
- }
- pages = PdfFile(filepath: "/some/path")
- DispatchQueue.global(qos: .background).async { in
- let image = pages.page(forIndex: index, scaleFactor: strongSelf.scaleFactor)
- DispatchQueue.main.async { () -> Void in
- completion(image)
- }
- }
Add Comment
Please, Sign In to add comment