如果想将多张图片、UIView拼接到一起,并返回UIImage,今天我们举例UIView,拼接UIImage,并生成UIImage。可以这样做:
首先,画UIView:
let vVIew =UIView(frame:CGRect(x:0, y:0, width:SCREEN_width, height:SCREEN_HEIGHT-hHeight))
vVIew.backgroundColor = UIColor.white
let iImageLogo =UIImageView(frame: .zero)
iImageLogo.frame=CGRect(x:0, y:0, width:120, height:40)
iImageLogo.center= vVIew.center
iImageLogo.image=UIImage(named:"logo")
vVIew.addSubview(iImageLogo)
将UIIView转成UIIamge
letvVIewSize =CGSize(width:SCREEN_width, height:SCREEN_HEIGHT-hHeight)
UIGraphicsBeginImageContextWithOptions(vVIewSize,false,0.0)
vVIew.layer.render(in: UIGraphicsGetCurrentContext()!)
let bottomImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
多张UIImage拼接
let size =CGSize(width:SCREEN_width, height:SCREEN_HEIGHT)
UIGraphicsBeginImageContextWithOptions(size,false,0.0)
let iImageRect =CGRect(x:0, y:0, width:SCREEN_width, height: hHeight)
iImage.draw(in: iImageRect)
let vViewRect =CGRect(x:0, y:hHeight, width:SCREEN_width, height:SCREEN_HEIGHT- hHeight)
bottomImage?.draw(in: vViewRect)
let imageC = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
这里补充说明,特别解释下UIGraphicsBeginImageContextWithOptions后面的参数:









网友评论