UIImage+Extension.swift
import UIKit
extension UIImage
{
// 改变图片的颜色
class func imageWithColor(imageName:String, color: UIColor) -> UIImage
{
let image = UIImage(named: imageName)!
UIGraphicsBeginImageContext(image.size)
let context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, image.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetBlendMode(context, .Normal);
let rect = CGRectMake(0, 0, image.size.width, image.size.height);
CGContextClipToMask(context, rect, image.CGImage);
color.setFill()
CGContextFillRect(context, rect);
let newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
}
private func creatBtn(imageName: String) -> UIButton
{
let btn = UIButton()
btn.setImage(UIImage.imageWithColor(imageName, color: UIColor.blackColor()), forState: .Normal)
return btn
}
获取上下文,图形上下文是什么意思?
CGContextRef context = UIGraphicsGetCurrentCont
画一个正方形图形 没有边框
CGContextSetRGBFillColor
CGContextFillRect(context, CGRectMake(2, 2, 270, 270));
CGContextStrokePath(context);
写文字
CGContextSetLineWidth(context, 1.0);
CGContextSetRGBFillColor
UIFont
[@"fangyp" drawInRect:CGRectMake(40, 40, 80, 20) withFont:font];
画一条线
CGContextSetRGBStrokeCol
CGContextMoveToPoint(context, 20, 20);
CGContextAddLineToPoint(context, 200,20);
CGContextStrokePath(context);
画正方形边框
CGContextSetRGBStrokeCol
CGContextSetLineWidth(context, 2.0);
CGContextAddRect(context, CGRectMake(2, 2, 270, 270));
CGContextStrokePath(context);
画方形背景颜色
UIGraphicsPopContext();
1、画线:在uiview类里重写下面方法
-(void)drawRect:(CGRect)rect
{
//