iOS 截屏 swift

余弘毅
2023-12-01
//        let bg = UIImageView(frame: self.view.frame)
//        bg.image = UIImage(named: "123")
//        self.view.addSubview(bg)
//        
//        // 截屏
//        // 开启位图上下文中
//        UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, false, 0)
//        
//        // 获取上下文
//        let ctx = UIGraphicsGetCurrentContext()
//        
//        // 把控件上的的图层渲染到上下文中
//        // layer只能渲染
//        view.layer.renderInContext(ctx!)
//        
//        // 生成一张图片
//        let image = UIGraphicsGetImageFromCurrentImageContext()
//        
//        // 把图片写到桌面上
//        let data = UIImagePNGRepresentation(image)
//        data?.writeToFile("/Users/liyanbin/Desktop/4/123.png", atomically: true)
 类似资料: