美文网首页
iOS 截图

iOS 截图

作者: 小白lf | 来源:发表于2021-04-09 14:23 被阅读0次
    public extension UIView {
        // 截取全图
        func snapshotImage(afterUpdates: Bool) -> UIImage? {
            UIGraphicsBeginImageContextWithOptions(bounds.size, isOpaque, 0)
            drawHierarchy(in: bounds, afterScreenUpdates: afterUpdates)
            let snap = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return snap
        }
        
        // 截取指定区域图
        func snapshotImage(in rect: CGRect, afterUpdates: Bool, scale: CGFloat = UIScreen.main.scale) -> UIImage? {
            let realRect = CGRect(x: rect.minX*scale, y: rect.minY*scale, width: rect.width*scale, height: rect.height*scale)
            
            let image = snapshotImage(afterUpdates: afterUpdates)
            guard let cgImage = image?.cgImage?.cropping(to: realRect) else { return nil }
            return UIImage(cgImage: cgImage)
        }
    }
    

    相关文章

      网友评论

          本文标题:iOS 截图

          本文链接:https://www.haomeiwen.com/subject/lfwikltx.html