iOS 图片截取(截屏)

2020-10-28  本文已影响0人  移动的键盘

/// 指定大小区域里生成图片

    /// - Parameters:

    ///  - image: 背景图

    ///  - size: 指定区域大小

    /// - Returns: 目标图片

    @objc public class func creatImage(image:UIImage,size:CGSize) -> UIImage {

        let view = UIView.init()

        view.bounds = CGRect(x:0, y:0, width: size.width, height: size.height)

        view.backgroundColor=UIColor(patternImage: image)

        UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale)

        view.layer.render(in: UIGraphicsGetCurrentContext()!)

        let newImage = UIGraphicsGetImageFromCurrentImageContext() ?? image

        UIGraphicsEndImageContext()

        return newImage

    }

    /// view 转化成图片

    /// - Parameter view: 当前View视图

    /// - Returns: 目标图片

    @objc public class func creatImage(view:UIView) -> UIImage {

        UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, UIScreen.main.scale)

        view.layer.render(in: UIGraphicsGetCurrentContext()!)

        let newImage = UIGraphicsGetImageFromCurrentImageContext() ?? UIImage.init()

        UIGraphicsEndImageContext()

       return newImage

    }

上一篇 下一篇

猜你喜欢

热点阅读