2

everyone!

I'm working in Swift 3, but if there is no solution in Swift 3 I would be willing to work in Swift 2.

I have a UICollectionView and a UIImageView that is contained in a UIView along with an image (and all this is contained in a UIScrollView, which means a lot of content is off-screen). The app I'm making will be useless if I can't figure out how to print out the contents of the UIView (or at the very least the UICollectionView) with the full content of the UICollectionView and the UIImageView.

I've been researching for a day and came up with the resources I will list below, but I still can't figure out how to accomplish my goal. Does anyone have any advice for me?

A possible alternative to printing out the Collection View would be to create a PDF or Image with the information contained within the collection view (one image and 3 labels per cell) without necessarily printing the collection view itself. If you can point out a way to do that, that would be equally helpful.

Thank you for your time!

My research results: (stack overflow wont let me post more than two links, so forgive the chopped up links) Apple documentation: (I can't make sense of this yet, but I'm trying to figure it out.) https://developer. apple .com /library/ios/documentation/2DDrawing/Conceptual/DrawingPrintingiOS/GeneratingPDF/GeneratingPDF.html

Crazy repository of frameworks, including pdf generators: github. com /vsouza/awesome-ios#pdf

How to convert a UIView to an image using swift: (I can't tell if this will take a screenshot of just what's on the page, or if it will take a screenshot of the whole view.) How to convert a UIview to a image

Apple function documentation: (This is a supplement to the above stackoverflow questions link.) developer. apple. com/library/ios/documentation/UIKit/Reference/UIKitFunctionReference/#//apple_ref/c/func/UIGraphicsGetImageFromCurrentImageContext

PDFGenerator: (This has the most potential, but I can't figure out how to use it yet.) https://github.com/sgr-ksmt/PDFGenerator

Community
  • 1
  • 1
retrovius
  • 782
  • 1
  • 10
  • 25

1 Answers1

0

PDFGenerator Features:

  • UIScrollView support : If view is UIScrollView, UITableView, UICollectionView, UIWebView, drawn whole content.

They also have a demo application on github.

Example fonte: ScrollView:

func generatePDF() {
    let v1 = UIScrollView(frame: CGRect(x: 0.0,y: 0, width: 100.0, height: 100.0))
    let v2 = UIView(frame: CGRect(x: 0.0,y: 0, width: 100.0, height: 200.0))
    let v3 = UIView(frame: CGRect(x: 0.0,y: 0, width: 100.0, height: 200.0))
    v1.backgroundColor = .red
    v1.contentSize = CGSize(width: 100.0, height: 200.0)
    v2.backgroundColor = .green
    v3.backgroundColor = .blue

    let dst = URL(fileURLWithPath: NSTemporaryDirectory().appending("sample1.pdf"))
    // outputs as Data
    do {
        let data = try PDFGenerator.generated(by: [v1, v2, v3])
        data.write(to: dst, options: .atomic)
    } catch (let error) {
        print(error)
    }

    // writes to Disk directly.
    do {
        try PDFGenerator.generate([v1, v2, v3], to: dst)    
    } catch (let error) {
        print(error)
    }
}