iOS-實現最簡單的畫線功能 . 轉


前提:CoreGraphics.framework 

- (void)viewDidLoad {  
    [super viewDidLoad];  
   
    UIImageView *imageView=[[UIImageView alloc] initWithFrame:self.view.frame];  
    [self.view addSubview:imageView];  
   
    self.view.backgroundColor=[UIColor blueColor];  
   
    UIGraphicsBeginImageContext(imageView.frame.size);  
    [imageView.image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)];  
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);  
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 15.0);  //線寬
    CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), YES);  
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);  //顏色  
    CGContextBeginPath(UIGraphicsGetCurrentContext());  
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), 100, 100);  //起點坐標
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), 200, 100);   //終點坐標
    CGContextStrokePath(UIGraphicsGetCurrentContext());  
    imageView.image=UIGraphicsGetImageFromCurrentImageContext();  
    UIGraphicsEndImageContext();  
}  

 

此處的RGB 用的時小數來表示,如灰色(0.8,0.8,0.8)

CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.8, 0.8, 0.8, 1.0);

 

http://marshal.easymorse.com/archives/4046


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM