ios - 选择 Collection View 单元格时如何在文档目录中传递文件 URL

标签 ios swift pdf swift3

我正在开发一款应用程序,可以将多个单页 pdf 合并为一个多页 pdf,并将它们显示在 UICollectionView 中。我将 .documentDirectory 的内容放入 [String] 中,以设置 UICollectionView 的数据源。一切都显示正常,但我无法将每个文件 URL 传递到一个数组中,我可以在我修改后用于合并文档的函数中使用该数组。我尝试使用它们来传递

let indexPath = self.collectionView.indexPathForSelectedItem

这不起作用,因为它只传递 indexPath Int 值,而且这也存在强制转换问题。

这是合并多个选定的pdf的功能。

func joinPDF(_ listOfPaths: [Any]) {
    var pdfPathOutput = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
    pdfPathOutput = pdfPathOutput.appending("/iScan_pdf_\(Int(Date().timeIntervalSince1970)).pdf")
    let pdfURLOutput: CFURL? = (URL(fileURLWithPath: pdfPathOutput) as CFURL?)
    var numberOfPages: Int = 0
    // Create the output context
    let writeContext = CGContext(pdfURLOutput!, mediaBox: nil, nil)
    for source in listOfPaths {
        let pdfURL: CFURL? = (URL(fileURLWithPath: source as! String) as CFURL?)
        //file ref
        let pdfRef: CGPDFDocument? = CGPDFDocument(pdfURL!)
        numberOfPages = pdfRef!.numberOfPages
        // Loop variables
        var page: CGPDFPage? = nil
        var mediaBox = CGRect.zero
        // Read the first PDF and generate the output pages
        // Finalize the output file
        print("GENERATING PAGES FROM PDF 1 (%@)...")
        for i in 1...numberOfPages {
            page = pdfRef?.page(at: i)
            mediaBox = page!.getBoxRect(.mediaBox)
            writeContext!.beginPage(mediaBox: &mediaBox)
            writeContext!.drawPDFPage(page!)
            writeContext!.endPage()
        }

    }
    writeContext!.closePDF()
}

我只需要一点帮助将选定的单元格文件 URL 放入数组中。我像这样创建了 UICollectionView 数据源。

 titles = try FileManager.default.contentsOfDirectory(atPath: documentsDirectories)

titles 是一个 [String] 然后我有另一个名为 images 的 var,它是一个 [UIImage]。我运行 for in 循环以从标题中获取图像,然后像这样设置数据源

 override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionViewCell
    cell.imageView.image = images[indexPath.item]

我有一个函数,可以从 pdf 创建缩略图,以便在 imageView 中显示它们。在函数 didSelectItemAt 中,我将其设置如下。

 override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
    if selected == true {
        let cell = collectionView.cellForItem(at: indexPath)
        cell?.layer.borderWidth = 2.0
        cell?.layer.borderColor = UIColor.red.cgColor

    }
}

这是我需要将项目输入数组的地方,只是不知道如何去做。

最佳答案

基本逻辑应该是这样的。

  1. 创建一个 arrayOfUrls 来保存选定的页面 url
  2. 当用户选择或取消选择时通过 arrayOfUrls 添加或删除 URL
  3. 当用户点击“完成”按钮时,调用 arrayOfUrls 的连接

还有更好的方法,但有了上面的信息,我可以帮助你这么多。 希望这有帮助...

问题更新后更新了答案

拥有多个数组违背了 OOP 的原因..下面是执行此操作的方法

创建一个包含所需变量的类...假设您需要图像、urlOfPDF 和 isSelected...

class PDFPage : NSObject {
    var title : String = ""
    var imageURL : String = "" //you can use uiimage object instead
    var urlOfPDF : NSNumber = -1
    var isSelected : Bool = false
}

在 Collection View 中重新加载数据之前,请先进行映射并将 PDFPage 的所有对象添加到数组中(例如 allPDFArray)...

像这样的东西......

for (int i = 0, i<titles.count, i++){
    var page: PDFPage = PDFPage()
    page.title = titles[i] 
    page.imageURL = set image for above title[i] here
    page.urlOfPDF = set url for above title[i] here
    page. isSelected = false
    allPDFArray.add(page)
}

现在您在本地拥有了所需的一切

在 cellForItemAt 中

let page = allPDFArray[indexpath.row] //now you have your all data in page obj
cell.imageView.image = page.image //or image url
cell.title.text = page.title
cell.isSelected can be used to highlight cell…you will need it as reloading cell will show you wrong results when you scroll

与 didSelectItemAt 类似

let page = allPDFArray[indexpath.row]
page.isSelected = page.isSelected
if page.isSelected then highlight

一旦用户点击完成... 只需浏览 allPDFArray 的所有元素,看看哪些元素被选中,并在此处创建一个数组,您可以将其发送到 joinPDF

关于ios - 选择 Collection View 单元格时如何在文档目录中传递文件 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42629532/

相关文章:

objective-c - 过渡到 ARC 的工具

ios - 将坐标转换为城市名称?

objective-c - 更改““YourApp”要使用您当前位置”的语言

iphone - 使用平移手势,单次触摸后忽略触摸?

c# - 在 C# 中检查打印机状态?

cordova - 在cordova应用程序中打开PDF

swift - Alamofire 的格式化字典

ios - 具有固定高度/宽度的自定大小的 UICollectionViewCell

swift - Swift Switch 会短路吗?

pdf - 如何在手机浏览器上显示pdf?