如何将任何项目拖入 PDFView?

问题描述

我想将任何项目添加到现有 PDF 中。

enter image description here

在这张图片中,您可以看到一个视图用于显示 PDF 文件,底部一个视图是显示所有图像的 CollectionView。

我想要的是用户可以将项目从 collectionView 拖放到 PDFView 中,用户可以导出更新的 PDF 文件。

我对 PDFView 使用了 Drop Interaction,对 Cell Image 项目使用了 Drag Interaction。 这是一个代码。


import UIKit
import PDFKit

class ViewController123: UIViewController {

    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var pdfView: PDFView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        guard let path = Bundle.main.url(forResource: "sample",withExtension: "pdf") else { return }
        if let document = PDFDocument(url: path) {
            pdfView.document = document
            pdfView.displayDirection = .horizontal
            pdfView.autoScales = true
            pdfView.displayMode = .singlePageContinuous
            let dropInteraction = UIDropInteraction(delegate: self)
            pdfView.documentView!.addInteraction(dropInteraction)
//            pdfView.usePageViewController(true,withViewOptions: [:])
        }
    }
}

extension ViewController123: UICollectionViewDelegate,UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
        return 8
    }
    
    func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImagesCell",for: indexPath) as? ImagesCell
        let dragInteraction = UIDragInteraction(delegate: self)
        dragInteraction.isEnabled = true
        cell?.imgItem.isUserInteractionEnabled = true
        cell?.imgItem.addInteraction(dragInteraction)
        return cell ?? ImagesCell()
    }
}

extension ViewController123: UIDropInteractionDelegate {
    func dropInteraction(_ interaction: UIDropInteraction,performDrop session: UIDropSession) {
        // Here check logic
        let dropLocation = session.location(in: pdfView.documentView!)
        session.loadObjects(ofClass: UIImage.self) {imageItems in
//            let images = imageItems as! [UIImage]
            if self.pdfView.documentView!.frame.contains(dropLocation) {
                let aView = UIView()
                aView.layer.borderWidth = 1
                aView.layer.borderColor = UIColor.gray.cgColor
                aView.layer.cornerRadius = 8
//                let imageView = UIImageView()
//                imageView.image = image
//                aView.addSubview(imageView)
                aView.frame = CGRect(x: dropLocation.x,y: dropLocation.y,width: 100,height: 50)
                self.pdfView.documentView?.addSubview(aView)
            }
        }
    }
    
    func dropInteraction(_ interaction: UIDropInteraction,sessionDidUpdate session: UIDropSession) -> UIDropProposal {
        return UIDropProposal(operation: .copy)
    }
}


extension ViewController123: UIDragInteractionDelegate {
    func dragInteraction(_ interaction: UIDragInteraction,itemsForBeginning session: UIDragSession) -> [UIDragItem] {
        let dropLocation = session.location(in: collectionView)
        if self.collectionView.frame.contains(dropLocation) {
            if let indexPath = self.collectionView.indexPathForItem(at: dropLocation) {
                let cell = self.collectionView.cellForItem(at: indexPath) as? ImagesCell
                guard let image = cell?.imgItem.image else { return [] }
                let provider = NSItemProvider(object: image)
                let item = UIDragItem(itemProvider: provider)
                item.localObject = image
                return [item]
            } else {
                return [UIDragItem(itemProvider: NSItemProvider(object: UIImage()))]
            }
        } else {
            return [UIDragItem(itemProvider: NSItemProvider(object: UIImage()))]
        }
    }
}


如果我使用 pdfView.usePageViewController(true,withViewOptions: [:]) 那么我得到 pdfView.documentView is nill

让我知道实现此功能的最简单方法。

这里是完整的代码:https://www.dropbox.com/s/4szmx07o7tjhs8v/DemoAlert.zip?dl=0

** 如果您有任何替代选项,请建议我只需要这样的功能,例如用户可以将任何图像或文本拖动到现有 PDF 中,并且可以调整大小并在删除该对象后移动整个 PDF 页面。**

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...