【问题标题】:Save incoming PDF in WebView to Memory将 WebView 中的传入 PDF 保存到内存
【发布时间】:2015-12-03 20:57:43
【问题描述】:

我有一个与打开 PDF 文件相关的项目。这是在Info.plist 中设置的。当我收到电子邮件中的 PDF 附件时,我可以将手指放在 PDF 附件上,然后在我的应用程序中“打开”。在我的AppDelegate 中,我添加了以下内容:

func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
    incomingTransfer = URL
    return true
}

incomingTransfer 是在另一个 ViewController 中声明为 NSURLGlobal Variable。这个ViewController 也有一个UIWebViewincomingTransfer 加载到它,我可以看到新的PDF 文件。我的目标是有一个按钮,允许用户将传入的 PDF 保存为 PDF。我遇到了麻烦。我以为我已经弄清楚了,但它根本没有保存为 PDF,而是保存为 String。有人能帮助我吗?我的目标是将传入的 PDF 文件作为 PDF 保存到应用程序内存中,最好保存在 DocumentDirectory 中。我很难将 Objective C 转换为 Swift。我保存它的原始代码是:

    let html = String(incomingFileTransfer)
    let fmt = UIMarkupTextPrintFormatter(markupText: html)

    let render = UIPrintPageRenderer()
    render.addPrintFormatter(fmt, startingAtPageAtIndex: 0)

    let page = CGRect(x: 0, y: 0, width: 595.2, height: 841.8) // A4, 72 dpi
    let printable = CGRectInset(page, 0, 0)

    render.setValue(NSValue(CGRect: page), forKey: "paperRect")
    render.setValue(NSValue(CGRect: printable), forKey: "printableRect")

    let pdfData = NSMutableData()
    UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, nil)

    for i in 1...render.numberOfPages() {

        UIGraphicsBeginPDFPage();
        let bounds = UIGraphicsGetPDFContextBounds()
        render.drawPageAtIndex(i - 1, inRect: bounds)
    }

    UIGraphicsEndPDFContext();

    recipeFileName = fileName.text!

    print("File Name Entered: \(recipeFileName)")

    let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]

    pdfData.writeToFile("\(documentsPath)/\(recipeFileName).pdf", atomically: true)

【问题讨论】:

    标签: ios pdf swift2 xcode7.1


    【解决方案1】:

    我想通了。我创建了一个名为“PDFFile”的class。在“PDFFile”中有两个variables,分别命名为var name: Stringvar url: NSURL。在我的'IncomingTransfer'ViewController 中,我有'保存'button 使用来自UITextField 的键入名称创建并保存新文件,并且我的AppDelegate 中指定的incomingURL 被分配给url @ 987654330@。然后使用NSCoding 将两者保存到PDFFile 类。然后,我从 PDFFile Class array 数据中为 dataSource 设置了一个 UITableView。当用户单击UITableViewCell 时,我创建了一个segue,然后使用UIWebView 转到一个新的ViewController。这个WebView 使用从NSCoding 保存的指定url 变量从urlRequest 加载PDF。

    AppDelegate代码:

    // Allows incoming file access (PDF)
    func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
    
        // Transfer incoming file to global variable to be read
    
        if url != "" {
    
            // Load from Mail App
    
            incomingFileTransfer = url
    
            incomingStatus = "Incoming"
    
        } else {
    
            // Regular Load
    
            print("App Delegate: No incoming file")
    
            incomingFileTransfer = nil
    
        }        
    
        return true
    }
    

    传入文件代码并保存button代码:

    // MARK: Properties
    var file: PDFFile?
    
    // MARK: Actions
    // Save Button
    let name = fileName.text ?? ""
    
    let url = incomingFileTransfer
    
    file = PDFFile(name: name, url: url)
    
    
    // MARK: NSCoding
    func saveFiles() {
    
        let isSuccessfulSave = NSKeyedArchiver.archiveRootObject(pdfFiles, toFile: PDFFile.ArchiveURL.path!)
    
        if !isSuccessfulSave {
    
            print("Failed to save PDF file")
    
        }
    
    }
    

    稍后查看保存的传入 PDF ViewController 代码: // 标记:属性 @IBOutlet 弱变量 pdfItemWebView: UIWebview!

    var incomingURL: NSURL!
    
    // Within ViewDidLoad
        if let file = file {
    
            pdfFileName.text = file.name
    
            incomingURL = file.url
    
            print("Saved URL: \(incomingURL)")
    
            print("Pending load...")
    
            let request = NSURLRequest(URL: incomingURL!)
    
            pdfItemWebView.loadRequest(request)
    
        }
    

    这很复杂,但对我有用。可能有更简单的方法,但这是我想出的方法,它可以满足我的需求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多