一.通过重写drawRect方法来绘制上下文
#import "MyView.h"
@implementation MyView
-(void)drawRect:(CGRect)rect {
//获取当前上下文
CGContextRef context =UIGraphicsGetCurrentContext();
// --------------------------实心圆
CGContextAddEllipseInRect(context,CGRectMake(0,0,50,50));
[[UIColorgreenColor]set];
CGContextFillPath(context);
// --------------------------空心圆
CGContextAddEllipseInRect(context,CGRectMake(60,0,50,50));
[[UIColorredColor]set];
CGContextSetLineWidth(context,10.0f);// 线的宽度
CGContextStrokePath(context);
// --------------------------椭圆
CGContextAddEllipseInRect(context,CGRectMake(120,0,80,50));
[[UIColorgreenColor]set];
CGContextFillPath(context);
// --------------------------直线
CGContextMoveToPoint(context,10,70); //起点
CGContextAddLineToPoint(context,100,70); //终点
CGContextAddLineToPoint(context,100,100);//终点
// CGContextSetRGBStrokeColor(ctx, 0, 1.0, 0, 1.0); //颜色
[[UIColorredColor]set]; //两种设置颜色的方式都可以
CGContextSetLineWidth(context,10.0f);// 线的宽度
CGContextSetLineCap(context,kCGLineCapRound);// 起点和重点圆角
CGContextSetLineJoin(context,kCGLineJoinRound);// 转角圆角
CGContextStrokePath(context);
// --------------------------三角形
CGContextMoveToPoint(context,10,150); //第一个点
CGContextAddLineToPoint(context,60,100); //第二个点
CGContextAddLineToPoint(context,100,150); //第三个点
[[UIColorpurpleColor]set];
CGContextClosePath(context);
CGContextStrokePath(context);
// --------------------------矩形
CGContextAddRect(context,CGRectMake(20,170,100, 50));
[[UIColororangeColor]set];
// CGContextStrokePath(ctx); //空心
CGContextFillPath(context);
// --------------------------圆弧
// *添加一个圆弧的上下文路径,可能之前直线段。”(x,y)”是弧的中心,“半径”是它的半径;“startAngle”是第一个角弧的端点;“endAngle”的第二个端点弧角;和“顺时针”1如果电弧是顺时针,否则为0。“startAngle”和“endAngle”是以弧度。* /
CGContextAddArc(context,200,170,50,0,M_PI,0);
// CGContextClosePath(context);
CGContextStrokePath(context);
// --------------------------文字
NSString *str =@"你在红楼,我在西游";
NSMutableDictionary *dict = [NSMutableDictionarydictionary];
dict[NSForegroundColorAttributeName] = [UIColorredColor];// 文字颜色
dict[NSFontAttributeName] = [UIFontsystemFontOfSize:14];//字体
[str drawInRect:CGRectMake(20,250,300, 30)withAttributes:dict];
// --------------------------图片
UIImage *img = [UIImageimageNamed:@"katong.jpg"];
// [img drawAsPatternInRect:CGRectMake(20, 280, 300, 300)]; //多个平铺
// [img drawAtPoint:CGPointMake(20, 280)]; //绘制到指定点,图片有多大就显示多大
[img drawInRect:CGRectMake(20,280,80,80)];//拉伸
}
@end
二.针对UIImage的操作
//--------------------------把整个屏幕转化为图片
UIGraphicsBeginImageContextWithOptions(self.view.frame.size,NO,0);
CGContextRef context =UIGraphicsGetCurrentContext();
//把当前的整个画面导入到context中
[self.view.layerrenderInContext:context];
UIImage* image =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//把图片存到本地
NSData * data =UIImageJPEGRepresentation([selfscaleImage:[UIImageimageNamed:@"katong.jpg"]toScale:0.1],0.1);
NSString *path = [NSStringstringWithFormat:@"%@/Documents/img12.jpg",NSHomeDirectory()];
[data writeToFile:pathatomically:NO];
NSLog(@"%@",path);
//裁剪图片
-(UIImage *)getImageFromImage{
//大图bigImage
//定义myImageRect,截图的区域
CGRect myImageRect =CGRectMake(10.0,10.0,200,200);
UIImage* bigImage= [UIImageimageNamed:@"katong.jpg"];
CGImageRef imageRef = bigImage.CGImage;
CGImageRef subImageRef =CGImageCreateWithImageInRect(imageRef, myImageRect);
UIImage* smallImage = [UIImageimageWithCGImage:subImageRef];
return smallImage;
}
//等比率缩放
- (UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize
{
UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize));
[image drawInRect:CGRectMake(0,0, image.size.width * scaleSize, image.size.height * scaleSize)];
UIImage *scaledImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
//自定长宽
- (UIImage *)reSizeImage:(UIImage *)image toSize:(CGSize)reSize
{
UIGraphicsBeginImageContext(CGSizeMake(reSize.width, reSize.height));
[image drawInRect:CGRectMake(0,0, reSize.width, reSize.height)];
UIImage *reSizeImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return reSizeImage;
}
三.Quartz2D
/************************** 线段 ******************/
/**
* 什么调用:当你视图第一次显示的时候就会调用
* 作用:绘图
* @param rect = self.bounds
*/
曲线
- (void)drawRect:(CGRect)rect
{
// 1.获取上下文
CGContextRef ctx =UIGraphicsGetCurrentContext();
// 2.拼接路径
UIBezierPath *path = [UIBezierPathbezierPath];
CGPoint startP =CGPointMake(10,125);
CGPoint endP =CGPointMake(240,125);
CGPoint controlP =CGPointMake(125,0);
[path moveToPoint:startP];
[path addQuadCurveToPoint:endPcontrolPoint:controlP];
// 3.把路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
// 4.渲染上下文到视图
CGContextStrokePath(ctx);
}
2条直线
- (void)draw2Line
{
// 1.获取上下文
CGContextRef ctx =UIGraphicsGetCurrentContext();
// 2.拼接路径
UIBezierPath *path = [UIBezierPathbezierPath];
// 设置起点
[path moveToPoint:CGPointMake(10,125)];
// 添加一条线到某个点
[path addLineToPoint:CGPointMake(230,125)];
// // 设置起点
// [path moveToPoint:CGPointMake(10, 10)];
//
// // 添加一条线到某个点
// [path addLineToPoint:CGPointMake(125, 100)];
UIBezierPath *path1 = [UIBezierPathbezierPath];
[path1 moveToPoint:CGPointMake(10,10)];
[path1 addLineToPoint:CGPointMake(125,100)];
// 3.把路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
CGContextAddPath(ctx, path1.CGPath);
// 设置绘图状态
// 设置线宽
CGContextSetLineWidth(ctx,10);
CGContextSetLineCap(ctx,kCGLineCapRound);
// CGContextSetRGBStrokeColor(ctx, 1, 0, 0, 1);
[[UIColorredColor] set];
// 4.渲染上下文到视图
CGContextStrokePath(ctx);
}
直线
- (void)drawLine
{
// 1.获取上下文
// CGContextRef CG CoreGraphics Ref引用
// 目前学的上下文都跟UIGraphics有关,以后想直接获取上下文,直接敲一个UIGraphics
CGContextRef ctx =UIGraphicsGetCurrentContext();
// 2.设置绘图信息(拼接路径)
UIBezierPath *path = [UIBezierPathbezierPath];
// 设置起点
[path moveToPoint:CGPointMake(10,10)];
// 添加一条线到某个点
[path addLineToPoint:CGPointMake(125,125)];
[path addLineToPoint:CGPointMake(240,10)];
// 3.把路径添加到上下文
// 直接把UIKit的路径转换成CoreGraphics,CG开头就能转
CGContextAddPath(ctx, path.CGPath);
// 4.把上下文渲染到视图
// Stroke描边
CGContextStrokePath(ctx);
}
/************************** 形状 ******************/
饼状
- (void)drawRect:(CGRect)rect
{
// Drawing code
// 1.获取上下文
CGContextRef ctx =UIGraphicsGetCurrentContext();
// 2.拼接路径
CGPoint center =CGPointMake(125,125);
CGFloat radius =100;
CGFloat startA =0;
CGFloat endA =M_PI_2;
UIBezierPath *path = [UIBezierPathbezierPathWithArcCenter:centerradius:radius startAngle:startAendAngle:endA clockwise:YES];
[path addLineToPoint:center];
// 3.把路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
// 4.渲染上下文
// CGContextStrokePath(ctx);
CGContextFillPath(ctx);
}
椭圆
- (void)drawArc
{
// 1.获取上下文
CGContextRef ctx =UIGraphicsGetCurrentContext();
// 2.拼接路径
CGPoint center =CGPointMake(125,125);
CGFloat radius =100;
CGFloat startA =0;
CGFloat endA =M_PI_2;
UIBezierPath *path = [UIBezierPathbezierPathWithArcCenter:centerradius:radius startAngle:startAendAngle:endA clockwise:YES];
// 3.把路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
// 4.渲染上下文
CGContextStrokePath(ctx);
}
圆形
- (void)drawCircle
{
// 1.获取上下文
CGContextRef ctx =UIGraphicsGetCurrentContext();
// 2.拼接路径
UIBezierPath *path = [UIBezierPathbezierPathWithOvalInRect:CGRectMake(10,10, 200, 100)];
// 3.把路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
// 4.渲染上下文
CGContextStrokePath(ctx);
}
矩形
- (void)drawRectangle
{
// 1.获取上下文
CGContextRef ctx =UIGraphicsGetCurrentContext();
// 2.拼接路径
UIBezierPath *path = [UIBezierPathbezierPathWithRect:CGRectMake(10,10, 200, 200)];
path = [UIBezierPathbezierPathWithRoundedRect:CGRectMake(10,10, 200, 200)cornerRadius:20];
// 3.把路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
// 4.渲染上下文
CGContextStrokePath(ctx);
}
三角
- (void)drawSupernene
{
// 1.获取上下文
CGContextRef ctx =UIGraphicsGetCurrentContext();
// 2.拼接路径
UIBezierPath *path = [UIBezierPathbezierPath];
CGPoint startP =CGPointMake(10,10);
[path moveToPoint:startP];
[path addLineToPoint:CGPointMake(125,125)];
[path addLineToPoint:CGPointMake(240,10)];
// 从路径的终点连接到起点
[path closePath];
// [path addLineToPoint:startP];
// 3.把路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
[[UIColorblueColor] setFill];
[[UIColorredColor] setStroke];
CGContextSetLineWidth(ctx,15);
// 4.渲染上下文
// CGContextStrokePath(ctx);
// CGContextFillPath(ctx);
// 即填充又描边 kCGPathFillStroke
CGContextDrawPath(ctx,kCGPathFillStroke);
}