drawRect中消除鋸齒,drawRect消除鋸齒

來源:互聯網
上載者:User

drawRect中消除鋸齒,drawRect消除鋸齒

在開始之前,我們需要建立一個DrawRectView

 

其初始代碼為

////  DrawRectView.h//  CGContextSetShouldAntialias////  Created by YouXianMing on 2017/8/30.//  Copyright  2017年 TechCode. All rights reserved.//#import <UIKit/UIKit.h>@interface DrawRectView : UIView@end
////  DrawRectView.m//  CGContextSetShouldAntialias////  Created by YouXianMing on 2017/8/30.//  Copyright  2017年 TechCode. All rights reserved.//#import "DrawRectView.h"@implementation DrawRectView- (instancetype)initWithFrame:(CGRect)frame {        if (self = [super initWithFrame:frame]) {                self.backgroundColor   = [UIColor clearColor];        self.layer.borderWidth = 0.5f;        self.layer.borderColor = [UIColor redColor].CGColor;    }        return self;}@end

 

在ViewController中使用(尺寸為100x100並置中)

 

顯示效果如下(用紅色邊框顯示邊界)

 

修改DrawRectView.m代碼如下

////  DrawRectView.m//  CGContextSetShouldAntialias////  Created by YouXianMing on 2017/8/30.//  Copyright  2017年 TechCode. All rights reserved.//#import "DrawRectView.h"@implementation DrawRectView- (instancetype)initWithFrame:(CGRect)frame {        if (self = [super initWithFrame:frame]) {                self.backgroundColor   = [UIColor clearColor];        self.layer.borderWidth = 0.5f;        self.layer.borderColor = [UIColor redColor].CGColor;    }        return self;}- (void)drawRect:(CGRect)rect {        // Set stroke color    [[UIColor blackColor] setStroke];        // Draw 7 lines.    for (int i = 0; i < 7; i++) {                UIBezierPath *path = [UIBezierPath bezierPath];        path.lineWidth     = 0.5f;        [path moveToPoint:CGPointMake(10, 10 + i * 10.3)];        [path addLineToPoint:CGPointMake(10 + 80, 10 + i * 10.3)];        [path stroke];    }}@end

 

其實就添加了下面的繪圖代碼而已,繪製7條線條,每條線條的寬度為0.5

 

效果如下

 

將圖片放大後會發現,線條的寬度並不一致,有的顏色深,有的顏色淺,這就是開了消除鋸齒之後的效果

 

修改代碼關閉消除鋸齒

////  DrawRectView.m//  CGContextSetShouldAntialias////  Created by YouXianMing on 2017/8/30.//  Copyright  2017年 TechCode. All rights reserved.//#import "DrawRectView.h"@implementation DrawRectView- (instancetype)initWithFrame:(CGRect)frame {        if (self = [super initWithFrame:frame]) {                self.backgroundColor   = [UIColor clearColor];        self.layer.borderWidth = 0.5f;        self.layer.borderColor = [UIColor redColor].CGColor;    }        return self;}- (void)drawRect:(CGRect)rect {        // Get context.    CGContextRef context = UIGraphicsGetCurrentContext();        // Sets anti-aliasing on.    CGContextSetShouldAntialias(context, NO);        // Set stroke color    [[UIColor blackColor] setStroke];        // Draw 7 lines.    for (int i = 0; i < 7; i++) {                UIBezierPath *path = [UIBezierPath bezierPath];        path.lineWidth     = 0.5f;        [path moveToPoint:CGPointMake(10, 10 + i * 10.3)];        [path addLineToPoint:CGPointMake(10 + 80, 10 + i * 10.3)];        [path stroke];    }}@end

 

顯示效果

 

圖片放大後,線條寬度一致

 

 

結論

開了消除鋸齒後,系統會對繪製的線條進行一定的模糊處理,來達到不容易看到狗牙的目的,什麼是狗牙?你可以運行以下代碼來看看兩者之間的區別

////  DrawRectView.m//  CGContextSetShouldAntialias////  Created by YouXianMing on 2017/8/30.//  Copyright  2017年 TechCode. All rights reserved.//#import "DrawRectView.h"@implementation DrawRectView- (instancetype)initWithFrame:(CGRect)frame {        if (self = [super initWithFrame:frame]) {                self.backgroundColor   = [UIColor clearColor];        self.layer.borderWidth = 0.5f;        self.layer.borderColor = [UIColor redColor].CGColor;    }        return self;}- (void)drawRect:(CGRect)rect {        // Get context.    CGContextRef context = UIGraphicsGetCurrentContext();        // Sets anti-aliasing off.    CGContextSetShouldAntialias(context, NO);        // Set stroke color    [[UIColor blackColor] setStroke];        // Draw 7 lines.    for (int i = 0; i < 7; i++) {                UIBezierPath *path = [UIBezierPath bezierPath];        path.lineWidth     = 0.5f;        [path moveToPoint:CGPointMake(0, 0 + i * 10.3)];        [path addLineToPoint:CGPointMake(10 + 80, 10 + i * 10.3)];        [path stroke];    }}@end

 

關閉消除鋸齒後不會出現模糊現象,都會出現鋸齒,俗稱狗牙

 

 

開啟消除鋸齒功能之後線條會模糊,鋸齒得到了一些緩解,稱作消除鋸齒

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.