Exclude View from Screenshot

后端 未结 3 1568
小鲜肉
小鲜肉 2021-01-03 10:10

This is how I take a screenshot of my view:

UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0)
view.drawViewHierarchyInRect(view.boun         


        
3条回答
  •  甜味超标
    2021-01-03 10:33

    Oh, actually, just following code (swift 4) can work. That is, create a context, and add the item you want in the snapshot. No need to add view in real screen.

    Note: currentView is the view which snapshot based on, commonly, it will be view of whole screen. And addViews are the views you want to add.

    func takeSnapShot(currentView: UIView , addViews: [UIView], hideViews: [UIView]) -> UIImage {
    
    
        for hideView in hideViews {
            hideView.isHidden = true
        }
    
        UIGraphicsBeginImageContextWithOptions(currentView.frame.size, false, 0.0)
    
        currentView.drawHierarchy(in: currentView.bounds, afterScreenUpdates: true)
        for addView in addViews{
            addView.drawHierarchy(in: addView.frame, afterScreenUpdates: true)
        }
    
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    
        for hideView in hideViews {
            hideView.isHidden = false
        }
    
        return image!
    }
    

提交回复
热议问题