ios - 分享 pdf 文件 .Swift
问题描述
我正在尝试共享我在 pdfView 中打开的 pdf 文档。但由于某种原因,activityViewController 是空的,我无法在某处发送文件。请告诉我如何修复我的代码!
我的 ViewController 的代码
pdfView.swift
import UIKit
import PDFKit
var nameFile:String?
var titleChapter:String?
class pdfView: UIViewController {
@IBOutlet weak var pdfDocView: PDFView!
// Share doc
@IBAction func shareDocAction(_ sender: UIBarButtonItem) {
let path = Bundle.main.path(forResource: nameFile, ofType:"pdf")
let pdfDocument = PDFDocument(url: URL(fileURLWithPath: path!))
var filesToShare = [Any]()
filesToShare.append(pdfDocument!)
let activityViewController = UIActivityViewController(activityItems: filesToShare , applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
present(activityViewController, animated: true, completion: nil)
}
//Pdf view
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = titleChapter
navigationController?.navigationBar.barTintColor = .white
//name Documents for view
if let path = Bundle.main.path(forResource: nameFile, ofType:"pdf") {
if let pdfDocument = PDFDocument(url: URL(fileURLWithPath: path)) {
pdfDocView.displayMode = .singlePageContinuous
pdfDocView.autoScales = true
pdfDocView.displayDirection = .vertical
pdfDocView.document = pdfDocument
pdfDocView.canZoomIn()
pdfDocView.canZoomOut()
}
}
}
}
解决方案
您还可以使用dataRepresentation
以下方式共享 PDFDocument:
func sharePDF(_ filePDF: PDFDocument) {
if let pdfData = filePDF.dataRepresentation() {
let objectsToShare = [pdfData]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
activityVC.popoverPresentationController?.sourceView = self.button
self.present(activityVC, animated: true, completion: nil)
}
}
推荐阅读
- python - 为什么 Pytorch 需要 DoubleTensor 而不是 FloatTensor?
- python - statsmodels 中的 AR 模型
- cuda - 使用推力在 GPU 上进行随机排列
- python - 猴子修补套接字库以使用特定的网络接口
- wso2 - 迁移到 WSO2 IS?如何将现有的密码哈希迁移到 MySql 之类的 Jdbc 存储中
- c# - 让所有列表条目出现在第二个 wpf 应用程序上
- r - GGanimate:带有数值的 geom_text 以十进制数字而不是整数进行动画处理
- python - 使用 tensorflow 后端指定 GPU ID
- c# - Identity Server (OpenID Connect) 混合流程:需要用户使用刷新令牌保持登录状态 5 年
- javascript - 为什么不应该使用内联 JavaScript?