/// 指定大小区域里生成图片
/// - 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
}
网友评论