Screen shot of View

I have a view that contains two imageviews and text that the user enters. Is it possible to either screenshot just the view or as another options convert the entire view into one image that is saved to Photos? Thanks for any help or suggestions.

Replies

What do you want exactly ?

- a partial screen capture, not the whole screen ?

- to do it programmatically ?


Have a look here, you should get enough knowledge to do it.

https://stackoverflow.com/questions/25448879/how-do-i-take-a-full-screen-screenshot-in-swift/40713286

Thanks. I'll take a look. Essentially it would be a partial screen shot, because it is the contents of a second view in the main screen. Either that or convert those contents into an image that could be saved- if that makes sense.

Yes, you can draw to pdf and save to file or display pdf on screen).


The code structure is as follows, for the func that draws the pdf and save:


func createPdfAndSave() -> Bool  {
    // 1. Create Print Formatter with input text.
    let formatter = UIMarkupTextPrintFormatter(markupText: "") // Some text textView.text)
   
    // 2. Add formatter with pageRender
   
    let render = MultiPagesUIPrintPageRenderer(howManyPages: 2)   // Chhose or compute number of pages
    render.addPrintFormatter(formatter, startingAtPageAt: 0)
   
    // 3. Assign paperRect and printableRect
   
    // device orientation is handled by constraints (in IB) 
    let page = CGRect(x: 0, y: 0, width: kPageWidth, height: kPageHeight) // 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")
   
    // 4. Initialize drawing areas
    defineSectionsTopCornerAndHeight()     // That a code you have to write
   
    // 5. Create PDF context and draw
    let rect = CGRect.zero
   
    let pdfData = NSMutableData()
    UIGraphicsBeginPDFContextToData(pdfData, rect, nil)

    render.prepare(forDrawingPages: NSMakeRange(0, render.numberOfPages))
    let bounds = UIGraphicsGetPDFContextBounds()
   
    let numberOnPage = 4          // Just to illustrate
    var setOfSectionsToPrint : Set
   
    for iPage in 1...render.numberOfPages { 
        currentPageOffset = kPageOffset * CGFloat(iPage - 1)
       
        UIGraphicsBeginPDFPage();
        render.drawPage(at: iPage - 1, in: bounds)     // That is the drawing code for the views
       
    } 
    UIGraphicsEndPDFContext();
   
    // 6. Save PDF file
   
    // https://forums.developer.apple.com/thread/100125
    do {
        let documentDirUrl = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
        let fileNameWithPDFExtension = "My first.pdf"
        let indexFileUrl = documentDirUrl.appendingPathComponent(fileNameWithPDFExtension)
        return pdfData.write(to: indexFileUrl, atomically: true)    // true if written
    } catch {
        print(error)
        return false
    }

}