0

告诉我如何将带有文档视图的文件传输到 PDF 文档。如果 PDF 文档需要 URL。我的是代码:

import UIKit
import PDFKit
import MobileCoreServices
import UniformTypeIdentifiers

class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate,
    PDFViewDelegate,UIDocumentPickerDelegate, UIDocumentMenuDelegate {        
    
    func documentMenu(_ documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
        documentPicker.delegate = self
        self.present(documentPicker, animated: true, completion: nil)
    }
    
    let pdf = PDFView()
    let newPDF = PDFView()
    
    @IBOutlet weak var pdfImage: UIView!
    @IBOutlet weak var imageGallery: UIImageView!        
    
    override func viewDidLoad() {
        super.viewDidLoad()
        pdfImage.addSubview(pdf)
      
        guard let url = Bundle.main.url(forResource: "test", withExtension: "pdf") else {
            return
        }
      
                
        guard let documentOne = PDFDocument(url:url) else { return }
        // guard let documentTwo = PDFDocument(url: urlTwo) else { return }

        pdf.document = documentOne
        pdf.autoScales = true
        pdf.delegate = self
        newPDF.delegate = self
     
        // create document
        let page = PDFPage(image: UIImage(named:"image")!)
                
        documentOne.insert(page!, at: 7)
        documentOne.write(to: url)             
    }
  
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        pdf.frame = pdfImage.bounds
        pdf.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    }
    
    @IBAction func addFileCloud(_ sender: Any) {
        openFile()
    }
    
    @IBAction func mergeButton(_ sender: Any) {
        guard let url = Bundle.main.url(forResource: "test", withExtension: "pdf") else {
            return
        }
    
        guard let documentOne = PDFDocument(url: url) else { return }

        pdf.autoScales = true
        pdf.delegate = self
           
        // create document
        let page = PDFPage(image: imageGallery.image!)
        documentOne.insert(page!, at: 7)
        pdf.document = documentOne
        documentOne.write(to: url)
    }
    
    @IBAction func ImageUpdate(_ sender: Any) {
        openGallery()
    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let pickedImage = info[.originalImage] as? UIImage {
            imageGallery.contentMode = .scaleAspectFit
            imageGallery.image = pickedImage
        }
        picker.dismiss(animated: true, completion: nil)
    }

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        self.dismiss(animated: true, completion: nil)
    }
    
    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        guard let addPDF = urls.first else {return}
          
        let deviceDoc = PDFDocument(url:addPDF)
        pdf.document = deviceDoc
        pdf.autoScales = true
        pdf.delegate = self
        deviceDoc?.write(to: addPDF)
        pdfImage.addSubview(pdf)
    }

    func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
        print("view was cancelled")
        dismiss(animated: true, completion: nil)
    }

    func openFile(){
        let supportedTypes : [UTType] = [UTType.pdf, UTType.image, UTType.png]
        let documentPicker = UIDocumentPickerViewController(forOpeningContentTypes: supportedTypes, asCopy: true)
        documentPicker.delegate = self
        documentPicker.modalPresentationStyle = .formSheet
        self.present(documentPicker, animated: true, completion: nil)
    }
    
    func openGallery() {
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.photoLibrary) {
            let imagePicker = UIImagePickerController()
            imagePicker.delegate = self
            // imagePicker.allowsEditing = true
            imagePicker.sourceType = UIImagePickerController.SourceType.photoLibrary
            self.present(imagePicker, animated: true, completion: nil)
        } else {
            let alert  = UIAlertController(title: "Warning", message: "You don't have permission to access gallery.", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
            self.present(alert, animated: true, completion: nil)
        }
    }
}

我的任务是将两个文件从两侧合并为一个。我在哪里上传图片。我在 Xcode 中插入了一个文件。现在我完成了选择我的文件并上传图片。我想合并它们。

4

0 回答 0