How to take screenshot of UIScrollView visible area?

前端 未结 8 1614
鱼传尺愫
鱼传尺愫 2021-02-13 16:57

How do I take a 1:1 screenshot of UIScrollView visible area? The content may be larger or smaller than UIScrollView bounds as well as half-hidden (I\'ve implemented custom scrol

8条回答
  •  盖世英雄少女心
    2021-02-13 17:30

    Update swift 3+, 4 on @Concuror code

    func getImage(fromCombinedContext background: UIView) -> UIImage {
            var image: UIImage?
            let size: CGSize = view.frame.size
            UIGraphicsBeginImageContext(size)
            background.layer.affineTransform()
            view.layer.render(in: UIGraphicsGetCurrentContext()!)
            image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            let imgRef = image?.cgImage?.cropping(to: background.frame)
            image = UIImage(cgImage: imgRef!)
            // CGImageRelease(imgRef!) // Removing on Swift - 'CGImageRelease' is unavailable: Core Foundation objects are automatically memory managed
            return image ?? UIImage()
        }
    

提交回复
热议问题