Search code examples
iosswiftuitableviewscreenshot

How can I get spesific tableviewcell screenshot with navigationbar?


I'm trying to get screenshot of uitableviewcell and I can succesfully do it. But I cannot append the navigationbar top of the UIImage.

This is what i have: https://i.sstatic.net/jphKQ.jpg

This is what i want to get: https://i.sstatic.net/RrHnd.png

How can i append navigation bar to image?

I've tried to create new UIImage for create merged image but it doesn't work.

At the below there is my code for get cell's screenshot properly.

        let cell = self.entryView.cellForRow(at: self.indexP) as! entryViewTableCell
        let frame = cell.frame
        let kalan = self.indexP.row % 2
        cell.entryText.backgroundColor = (kalan == 0) ? Theme.cellFirstColor : Theme.cellSecondColor
        cell.entryText.text = ""
        UIGraphicsBeginImageContextWithOptions(cell.entryText.frame.size, true, 2)
        cell.entryText.attributedText = self.entryler[self.indexP.row]
        cell.entryText.textColor = Theme.labelColor
        cell.entryText.tintColor = Theme.linkColor
        cell.entryText.layer.render(in: UIGraphicsGetCurrentContext()!)
        let snapshot = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()

edit: https://i.sstatic.net/agsT3.jpg

I can get this image with below code, but navigationbar opacity too low. How can I opaque it?

        let cell = self.entryView.cellForRow(at: self.indexP) as! entryViewTableCell
        let frame = cell.entryText.frame
        let kalan = self.indexP.row % 2
        cell.entryText.backgroundColor = (kalan == 0) ? Theme.cellFirstColor : Theme.cellSecondColor
        cell.entryText.text = ""
        UIGraphicsBeginImageContextWithOptions(frame.size, true, 2)
        cell.entryText.attributedText = self.entryler[self.indexP.row]
        cell.entryText.textColor = Theme.labelColor
        cell.entryText.tintColor = Theme.linkColor
        cell.entryText.layer.render(in: UIGraphicsGetCurrentContext()!)
        let snapshot = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()

        UIGraphicsBeginImageContextWithOptions((self.navigationController?.navigationBar.layer.frame.size)!, true, 2)
            self.navigationController?.navigationBar.drawHierarchy(in: (self.navigationController?.navigationBar.bounds)!, afterScreenUpdates: false)

            self.navigationController?.navigationBar.layer.render(in: UIGraphicsGetCurrentContext()!)
            let ss = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()

            let img = self.getMixedImg(image1: ss!, image2: snapshot!)


func getMixedImg(image1: UIImage, image2: UIImage) -> UIImage? {

    let size = CGSize(width: image1.size.width, height: image1.size.height + image2.size.height)

    UIGraphicsBeginImageContextWithOptions(size, true, 2)

    image1.draw(in: CGRect(x: 0,y: 0,width: size.width, height: image1.size.height))
    image2.draw(in: CGRect(x: 0,y: image1.size.height,width: size.width, height: image2.size.height))
    let finalImage = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()

    return finalImage
}

Solution

  • This is answer for my question:

    let cell = self.entryView.cellForRow(at: self.indexP) as! entryViewTableCell
            let frame = cell.entryText.frame
            let kalan = self.indexP.row % 2
            cell.entryText.backgroundColor = (kalan == 0) ? Theme.cellFirstColor : Theme.cellSecondColor
            cell.entryText.text = ""
            UIGraphicsBeginImageContextWithOptions(frame.size, true, 3)
            cell.entryText.attributedText = self.entryler[self.indexP.row]
            cell.entryText.textColor = Theme.labelColor
            cell.entryText.tintColor = Theme.linkColor
            cell.entryText.layer.render(in: UIGraphicsGetCurrentContext()!)
            let snapshot = UIGraphicsGetImageFromCurrentImageContext()
                UIGraphicsEndImageContext()
    
            UIGraphicsBeginImageContextWithOptions((self.navigationController?.navigationBar.layer.bounds.size)!, true, 3)
                self.navigationController?.navigationBar.drawHierarchy(in: (self.navigationController?.navigationBar.layer.bounds)!, afterScreenUpdates: false)
                let ss = UIGraphicsGetImageFromCurrentImageContext()
                UIGraphicsEndImageContext()
    
                let img = self.getMixedImg(image1: ss!, image2: snapshot!)
    func getMixedImg(image1: UIImage, image2: UIImage) -> UIImage? {
    
        let size = CGSize(width: image1.size.width, height: image1.size.height + image2.size.height)
    
        UIGraphicsBeginImageContextWithOptions(size, true, 2)
    
        image1.draw(in: CGRect(x: 0,y: 0,width: size.width, height: image1.size.height))
        image2.draw(in: CGRect(x: 0,y: image1.size.height,width: size.width, height: image2.size.height))
        let finalImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
    
        return finalImage
    }
    

    and result: https://i.sstatic.net/zS0Zw.jpg