Quartz2D绘图,边框效果

在viewController里,添加

 1   //创建一个基于UIImage的图形上下文
 2     UIGraphicsBeginImageContext(CGSizeMake(320, 450));
 3     //取出“当前”上下文,也就是上句话创建的上下文,返回CGContextRef类型
 4     CGContextRef ctx = UIGraphicsGetCurrentContext();
 5     //开始向上下文中增加路径:即开始绘图
 6     CGContextBeginPath(ctx);
 7     //画圆,第一个参数为上下文,第二个和第三个为圆点得xy坐标,第四个为圆半径,第五个为开始的弧度,第六个为结束的弧度,第七个为顺时针和逆时针(对应0和1)
 8     CGContextAddArc(ctx, 80, 90, 30, 0, 2*M_PI, 0);
 9     //设置填充颜色
10     CGContextSetRGBFillColor(ctx, 0.8, 0, 0, 1);
11     //填充颜色
12     CGContextFillPath(ctx);
13     //从上下文中取得UIImage对象
14     UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
15     //绘图完毕,撤销上下文
16     UIGraphicsEndImageContext();
17     
18     UIImageView *imageView = [[UIImageView alloc] initWithImage:img];
19     [self.view addSubview:imageView];

对于图形上下文的理解;

1.图形上下文是对战结构

2.UIGraphicsBeginImageContext新建的图形上下文位于堆栈顶端

3.UIGraphicsGetCurrentContext就是取出当前栈顶元素,也就是返回最后一次新建的上下文

4.UIGraphicsEndImageContext为去除当前栈顶元素,也就是撤销最后一次建立的上下文

同理,CGContextAddArcToPoint为画弧线,CGContextAddRect为画矩形,CGContextFillRect为画实心矩形,CGContextMoveToPoint和CGContextAddLineToPoint为画直线

边框:

 1 - (void)drawRect:(CGRect)rect
 2 {
 3     CGContextRef context = UIGraphicsGetCurrentContext();
 4     CGContextSetLineCap(context, kCGLineCapRound);
 5     CGContextSetLineWidth(context, 1.0f);
 6     CGContextSetRGBStrokeColor(context, 1, 1, 1, 1);
 7     CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
 8     CGContextBeginPath(context);
 9     CGContextMoveToPoint(context, 0, 0);
10     CGContextAddLineToPoint(context,  self.frame.size.width, 0);
11     CGContextAddLineToPoint(context,  self.frame.size.width, 40);
12     CGContextAddLineToPoint(context, 0, 40);
13     CGContextAddLineToPoint(context, 0, 0);
14     CGContextStrokePath(context);
15     
16 }

一般可以直接设置;

myWebView.layer.borderWidth = 5;
          
myWebView.layer.borderColor = [[UIColor colorWithRed:0.52 green:0.09 blue:0.07 alpha:1] CGColor];
 

 

 

//根据view返回image
+ (UIImage*) imageWithUIView:(UIView*) view{
    UIGraphicsBeginImageContext(view.bounds.size);
    CGContextRef currnetContext = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:currnetContext];
    UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

 

 

posted on 2014-04-28 17:36  李园春秋  阅读(365)  评论(0编辑  收藏  举报

导航