1、旋转
2、裁剪
3、截取
4、平铺
自由拉伸
func imageWithResizedImage(name:String,width:CGFloat,height:CGFloat) ->UIImage? {
let image = UIImage(named: name)
return image?.stretchableImage(withLeftCapWidth: Int(width), topCapHeight: Int(height))
}
等比例缩放
func imageWithOriginImage(image:UIImage,defineWidth:CGFloat)->UIImage? {
let imageSize = image.size
let width = imageSize.width
let height = imageSize.height
let targetWidth = defineWidth
let targetHeight = height / (width / targetWidth)
let size = CGSize(width: targetWidth, height: targetHeight)
UIGraphicsBeginImageContext(size)//size 为CGSize类型,即你所需要的图片尺寸
image.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return scaledImage
}
根据颜色生成图片
func imageWithColor(color:UIColor,size:CGSize) ->UIImage? {
let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
//开启位图上下文
UIGraphicsBeginImageContext(rect.size)
//获取上下文
let context = UIGraphicsGetCurrentContext()
context?.setFillColor(color.cgColor)
context?.fill(rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
//关闭上下文
UIGraphicsEndImageContext()
return image
}
截取某个view视图
func imageWithCaptureView(captureView:UIView) ->UIImage? {
//开启位图上下文
UIGraphicsBeginImageContextWithOptions(captureView.bounds.size, false, 0)
//获取上下文
if let ctx = UIGraphicsGetCurrentContext() {
//把控件上的图层渲染到上下文
captureView.layer.render(in: ctx)
let screenImage = UIGraphicsGetImageFromCurrentImageContext()
//关闭上下文
UIGraphicsEndImageContext()
return screenImage
}
return nil
}
文字水印
func imageWithWaterMarkImage(imageName:String,str:String,point:CGPoint,dict:NSDictionary) ->UIImage? {
if let image = UIImage(named: imageName) {
UIGraphicsBeginImageContextWithOptions(image.size, false, 0)
//把图片画上去
image.draw(at: CGPoint.zero)
//绘制文字到图片
(str as! NSString).draw(at: point, withAttributes: dict as! [NSAttributedStringKey : Any])
// 从上下文获取图片
let imageWater = UIGraphicsGetImageFromCurrentImageContext()
//关闭上下文
UIGraphicsEndImageContext()
return imageWater
}
return nil
}
图片水印
func imageWithWaterMarkImage(image:UIImage,waterImage:UIImage,rect:CGRect) ->UIImage? {
//1.开启上下文
UIGraphicsBeginImageContextWithOptions(image.size, false, 0)
//2.绘制背景图片
image.draw(in: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height))
//绘制水印图片到当前上下文
waterImage.draw(in: rect)
//3.从上下文中获取新图片
let newImage = UIGraphicsGetImageFromCurrentImageContext()
//4.关闭图形上下文
UIGraphicsEndImageContext()
//返回图片
return newImage
}
图片切圆
func imageWithClipImage(image:UIImage,borderWidth:CGFloat,color:UIColor) ->UIImage? {
// 图片的宽度和高度
let imageWH = image.size.width
// 设置圆环的宽度
let border = borderWidth
// 圆形的宽度和高度
let ovalWH = imageWH + 2 * border
// 1.开启上下文
UIGraphicsBeginImageContextWithOptions(CGSize(width: ovalWH, height: ovalWH), false, 0)
// 2.画大圆
let path = UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: ovalWH, height: ovalWH))
color.set()
path.fill()
// 3.设置裁剪区域
let clipPath = UIBezierPath(ovalIn: CGRect(x: border, y: border, width: imageWH, height: imageWH))
clipPath.addClip()
// 4.绘制图片
image.draw(at: CGPoint(x: border, y: border))
// 5.获取图片
let clipImage = UIGraphicsGetImageFromCurrentImageContext()
// 6.关闭上下文
UIGraphicsEndImageContext()
return clipImage
}
网友评论