IOS影像處理(3)繪製路徑

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   使用   for   

通過路徑我們可以實現更加複雜的圖形的繪製,比如多邊形,弧,圓角矩形等等

- (void)drawRect:(CGRect)rect{    //擷取映像內容物件    CGContextRef context = UIGraphicsGetCurrentContext();    CGContextSetRGBStrokeColor(context, 250/255.0, 250/255.0, 250/255.0, 1);    CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);    CGContextSetShadowWithColor(context, CGSizeMake(1, 1), 3, [UIColor yellowColor].CGColor);    CGContextSetLineWidth(context, 2);        //添加直線(方式1)    //1,2點組成第一條線段,2,3點組成第二條線段,3,4點組成第3條線段,以此類推    CGPoint points[] = {CGPointMake(50, 15),CGPointMake(150, 15),CGPointMake(50, 25),CGPointMake(100, 25),CGPointMake(200, 25)};    CGContextAddLines(context, points, 5);    CGContextDrawPath(context, kCGPathStroke);            //添加直線(方式2)    CGContextMoveToPoint(context, 0, 40);    CGContextAddLineToPoint(context, 100, 40);    CGContextAddLineToPoint(context, 100, 50);    //閉合路徑,串連終點和起點    CGContextClosePath(context);    CGContextDrawPath(context, kCGPathStroke);            //添加矩形    CGContextAddRect(context, CGRectMake(150, 50, 100, 30));    CGContextDrawPath(context, kCGPathFill);            //添加橢圓    CGContextAddEllipseInRect(context, CGRectMake(0, 50, 100, 30));    CGContextDrawPath(context, kCGPathStroke);            //添加弧    //最後一個參數代表方向,0表示順時針,1表示逆時針    CGContextAddArc(context, 100, 150, 30, -90*M_PI/180, 90*M_PI/180, 1);    CGContextDrawPath(context, kCGPathStroke);        //添加二次函數曲線    CGContextMoveToPoint(context, 300, 200);    CGContextAddQuadCurveToPoint(context, 160, 300, 320, 300);    CGContextDrawPath(context, kCGPathStroke);        //添加貝茲路徑    CGContextMoveToPoint(context, 10, 200);    CGContextAddCurveToPoint(context, 160, 230, 0, 300, 50, 400);    CGContextDrawPath(context, kCGPathStroke);        }

 

運行結果

 

由上例可以看出使用路徑可以很方便的繪製基礎的幾何圖形以及畫弧,通過組合這些幾何圖形可以實現更加複雜的映像,下面的代碼分別實現了圓角矩形,多角形的繪製

//繪製圓角矩形//rect表示矩形地區,radius表示圓角半徑void drawRoundrect(CGContextRef context,CGRect rect,CGFloat radius) {    CGPoint origin = rect.origin;    CGSize size = rect.size;    CGContextMoveToPoint(context, origin.x + radius, origin.y);    //添加上邊    CGContextAddLineToPoint(context, origin.x + size.width - radius, origin.y);    //添加右上方弧    CGContextAddArcToPoint(context, origin.x + size.width, origin.y, origin.x + size.width, origin.y + radius, radius);    //添加右邊    CGContextAddLineToPoint(context, origin.x + size.width, origin.y + size.height - radius);    //添加右下角弧    CGContextAddArcToPoint(context, origin.x + size.width, origin.y + size.height, origin.x + size.width - radius, origin.y + size.height, radius);    //添加底邊    CGContextAddLineToPoint(context, origin.x + radius, origin.y + size.height);    //添加左下角弧    CGContextAddArcToPoint(context, origin.x, origin.y + size.height, origin.x, origin.y + size.height - radius, radius);    //添加左邊    CGContextAddLineToPoint(context, origin.x, origin.y + radius);    //添加左上方弧    CGContextAddArcToPoint(context, origin.x, origin.y, origin.x + radius, origin.y, radius);    }- (void)drawRect:(CGRect)rect{    //擷取映像內容物件    CGContextRef context = UIGraphicsGetCurrentContext();    CGContextSetRGBStrokeColor(context, 1,1,1, 0.7);    CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);    CGContextSetLineWidth(context, 2);        drawRoundrect(context,CGRectMake(20, 20, 200, 150), 20);    CGContextDrawPath(context, kCGPathStroke);}

運行結果 

 

//繪製多角形//centerPoint代表中心點,size代表邊的長度,count代表角的個數void drawMulAngle(CGContextRef context,CGPoint centerPoint,CGFloat size,int count) {    CGFloat dig = 4 * M_PI / count;        CGContextMoveToPoint(context, centerPoint.x, centerPoint.y + size);    for (int i = 1; i <= count; i++) {        CGFloat x = sin(i*dig) * size + centerPoint.x;        CGFloat y = cos(i*dig) * size + centerPoint.y;                CGContextAddLineToPoint(context, x, y);    }}- (void)drawRect:(CGRect)rect{    //擷取映像內容物件    CGContextRef context = UIGraphicsGetCurrentContext();    CGContextSetRGBStrokeColor(context, 1,1,1, 0.7);    CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);    CGContextSetLineWidth(context, 2);    CGContextBeginPath(context);    drawMulAngle(context,CGPointMake(160, 200), 50,5);    CGContextClosePath(context);    CGContextDrawPath(context, kCGPathFillStroke);}

 

運行結果

IOS影像處理(3)繪製路徑

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.