2016-10-13 6 views
0

IOS上のSwiftを使用して簡単なPDF文書を共有すると、奇妙な動作が発生します。基本的に私がそれを作成し、それを印刷するために共有しなければならないイメージは含まれていません。 UIViewControllerを使用して最初に表示してから共有しても問題ありません。私はちょうど理由を取得していない!ここでIOSスウィフトで画像を共有するPDF文書

は、私のコードのinterresting部分です:

func getHtml() -> String { 
    // Create a HTML document to be printed 
    // Save data to file 
    let fileName = "noteImage.jpeg" 
    let pathToInvoiceHTMLTemplate = Bundle.main.path(forResource: "note", ofType: "html") 
    let tmpDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory()) 
    let fileURL = tmpDirectoryURL.appendingPathComponent(fileName) 
    let mergedImages = getMergedImages() 
    //let pngImageData = UIImagePNGRepresentation(image) 
    let imageData = UIImageJPEGRepresentation(mergedImages, 1.0) // if you want to save as JPEG 
    try? imageData!.write(to: URL(fileURLWithPath: fileURL.path), options: [.atomic]) 
    var htmlText = "<html><body><b>Problem Retrieving Note Template</b></body></html>" 

    do { 
     // Load the note HTML template code into a String variable. 
     htmlText = try String(contentsOfFile: pathToInvoiceHTMLTemplate!) 

     // Replace the variables in HTML. 
     htmlText = htmlText.replacingOccurrences(of: "__PROJECT_NAME__", with: projectName!) 
     htmlText = htmlText.replacingOccurrences(of: "__NOTE_NAME__", with: note!.name) 
     htmlText = htmlText.replacingOccurrences(of: "__NOTE_IMAGE__", with: "file:"+fileURL.path) 
    } 
    catch { 
     print("Unable to open and use HTML template files.") 
    } 
    return htmlText 
} 

func getPdf() -> NSMutableData { 
    // Create a PDF document from the HTML to be shared 
    // Format HTML 
    let fmt = UIMarkupTextPrintFormatter(markupText: getHtml()) 
    // Assign print formatter to UIPrintPageRenderer 
    let render = UIPrintPageRenderer() 
    render.addPrintFormatter(fmt, startingAtPageAt: 0) 
    // Assign paperRect and printableRect 
    let page = CGRect(x: 0, y: 0, width: 595.2, height: 841.8) // A4, 72 dpi 
    let printable = page.insetBy(dx: 0, dy: 0) 
    render.setValue(NSValue(cgRect: page), forKey: "paperRect") 
    render.setValue(NSValue(cgRect: printable), forKey: "printableRect") 
    // Create PDF context and draw 
    let pdfData = NSMutableData() 
    UIGraphicsBeginPDFContextToData(pdfData, CGRect.zero, nil) 
    for i in 1...render.numberOfPages { 
     UIGraphicsBeginPDFPage(); 
     let bounds = UIGraphicsGetPDFContextBounds() 
     render.drawPage(at: i - 1, in: bounds) 
    } 
    UIGraphicsEndPDFContext(); 
    return pdfData 
} 

@IBAction func shareNote(_ sender: UIBarButtonItem) { 
    // Called in direct sharing 
    let firstActivityItem = "Text int the message" 
    let docToShare = getPdf()   
    let activityViewController : UIActivityViewController = UIActivityViewController(
     activityItems: [firstActivityItem, docToShare], applicationActivities: nil) 

    // This lines is for the popover you need to show in iPad 
    activityViewController.popoverPresentationController?.barButtonItem = sender 

    // This line remove the arrow of the popover to show in iPad 
    activityViewController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection() 
    activityViewController.popoverPresentationController?.sourceRect = CGRect(x: 150, y: 150, width: 0, height: 0) 

    // Anything you want to exclude 
    activityViewController.excludedActivityTypes = [ 
     UIActivityType.postToWeibo, 
     UIActivityType.assignToContact, 
     UIActivityType.saveToCameraRoll, 
     UIActivityType.addToReadingList, 
     UIActivityType.postToFlickr, 
     UIActivityType.postToVimeo, 
     UIActivityType.postToTencentWeibo, 
    ] 

    self.present(activityViewController, animated: true, completion: nil) 
} 

@IBAction func shareDocument(_ sender: UIBarButtonItem) { 
    // Called in the preview controller when the HTML is displayed 
    let firstActivityItem = "Text in the message" 
    let docToShare = getPdf() 
    let activityViewController : UIActivityViewController = UIActivityViewController(
     activityItems: [firstActivityItem, docToShare], applicationActivities: nil) 

    // This lines is for the popover you need to show in iPad 
    activityViewController.popoverPresentationController?.barButtonItem = sender 

    // This line remove the arrow of the popover to show in iPad 
    activityViewController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection() 
    activityViewController.popoverPresentationController?.sourceRect = CGRect(x: 150, y: 150, width: 0, height: 0) 

    // Anything you want to exclude 
    activityViewController.excludedActivityTypes = [ 
     UIActivityType.postToWeibo, 
     UIActivityType.assignToContact, 
     UIActivityType.saveToCameraRoll, 
     UIActivityType.addToReadingList, 
     UIActivityType.postToFlickr, 
     UIActivityType.postToVimeo, 
     UIActivityType.postToTencentWeibo, 
    ] 

    self.present(activityViewController, animated: true, completion: nil)   
} 

誰もが手掛かりを持っていますか?

答えて

0

私の問題の回避策を見つけました: 私が今までに見つけた唯一の解決策は、私が隠すダミーのUIWebViewを作成することです。私はUIWebViewでHTMLを読み込んでからPDFを作成します。ここに私のコードです:

override func viewDidLoad() { 
    super.viewDidLoad() 

    hiddenWebView.loadHTMLString(htmlText, baseURL: nil) 
    hiddenWebView.isHidden = true 
} 

override func viewDidAppear(_ animated: Bool) { 
    pdfDocument = getPdf() 
    let tmpDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory()) 
    let fileURL = tmpDirectoryURL.appendingPathComponent("document.pdf") 
    pdfDocument.write(to: fileURL, atomically: false) 
    documentWebView.loadRequest(URLRequest(url: fileURL)) 
} 
関連する問題