標籤:
常說溫故而知新,時隔這麼久再次看這些基礎內容仍然很興奮!把敲的代碼分享出來,希望能對讀者提供一丁點啟發
/** * 裁剪圓形圖片 */-(void)circleIcon{ UIImage *image = [UIImage imageNamed:@"baby"]; UIGraphicsBeginImageContext(image.size); UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, image.size.width, image.size.height)]; [path addClip]; [image drawAtPoint:CGPointZero]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); self.imageV.image = newImage;}/** * 圖片加浮水印 */-(void)waterMark{ UIImage *image= [UIImage imageNamed:@"baby"]; //開啟圖片上下文 UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0); //將圖片繪製到圖片上下文 [image drawAtPoint:CGPointZero]; //繪製文字 NSString *str = @"邱學偉"; [str drawAtPoint:CGPointZero withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20]}]; //產生圖片 UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); //手動關閉上下文 UIGraphicsEndImageContext(); self.imageV.image = newImage;}-(void)awakeFromNib{// [self timer];}/** * 圖形上下文矩陣操作 */-(void)drawMatrix{ //1.開啟一個上下文 CGContextRef CTX = UIGraphicsGetCurrentContext(); //2.繪製路徑 UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 100, 200, 100)]; [[UIColor redColor] set]; //上下文矩陣操作 //平移// CGContextTranslateCTM(CTX, -50, -50); //縮放 CGContextScaleCTM(CTX, 0.5, 0.5); //旋轉 CGContextRotateCTM(CTX, M_PI_4); //3.將路徑添加到圖形上下文 CGContextAddPath(CTX, path.CGPath); //4.把上下水渲染出來 CGContextFillPath(CTX);}/** * 定時器下雪 */-(void)timer{ //定時器方法1-卡頓// NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(updateSnow) userInfo:nil repeats:YES]; //定時器方法2-在螢幕每次重新整理(沒秒重新整理60次)時調用 CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateSnow)]; [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];}-(void)updateSnow{ _Y+=10; if (_Y > self.bounds.size.height) { _Y = 0; } [self setNeedsDisplay];}static int _Y = 0;/** * 畫圖片 */-(void)drawImageWith:(CGRect)rect{ UIImage *image = [UIImage imageNamed:@"snow"]; //保持圖片源尺寸 [image drawAtPoint:CGPointMake(10, _Y)]; //填充整個View// [image drawInRect:self.bounds]; //把image畫滿整個View// [image drawAsPatternInRect:self.bounds];}/** * 畫文字 */-(void)drawTextWith:(CGRect)rect{ NSString *str = @"邱學偉"; NSMutableDictionary *attri = [NSMutableDictionary dictionary]; [attri setObject:[UIFont systemFontOfSize:50] forKey:NSFontAttributeName]; [attri setObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName]; //不會自動換行 [str drawAtPoint:CGPointZero withAttributes:attri]; //會自動換行 [str drawInRect:rect withAttributes:attri];}/** * 畫餅圖 */-(void)drawPreWithRect:(CGRect)rect{ NSArray *angleArr = @[@25,@25,@50]; CGPoint center = CGPointMake(rect.size.width*0.5, rect.size.height*0.5); CGFloat radius = rect.size.width*0.5-10; CGFloat Angle = 0; CGFloat startAngle = 0; CGFloat endAngle = 0; for (NSNumber *num in angleArr) { startAngle = endAngle; Angle = num.intValue/100.0*M_PI*2; endAngle = startAngle + Angle; UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES]; [path addLineToPoint:center]; [[self randomColor] set]; [path fill]; }}//隨機顏色-(UIColor *)randomColor{ CGFloat r = arc4random_uniform(256) / 255.0; CGFloat g = arc4random_uniform(256) / 255.0; CGFloat b = arc4random_uniform(256) / 255.0; return [UIColor colorWithRed:r green:g blue:b alpha:1];}/** * 畫進度條 */-(void)setProgressValue:(CGFloat)progressValue{ if (_progressValue != progressValue) { _progressValue = progressValue; [self setNeedsDisplay]; }}-(void)drawProgressWithRect:(CGRect)rect{ CGPoint center = CGPointMake(rect.size.width*0.5, rect.size.height*0.5); CGFloat radius = rect.size.width*0.5-10; CGFloat startAngle = -M_PI_2; CGFloat endAngle = -M_PI_2 + self.progressValue*M_PI*2; UIBezierPath *progressPath = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES]; [progressPath stroke];}/** * 畫圓和圓弧 */-(void)drawCircleWithRect:(CGRect)rect{ //1. radius - 圓角半徑 UIBezierPath *circel0 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 20, 100, 100) cornerRadius:10]; // [circel0 stroke]; UIBezierPath *circle1 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(20, 20, 100, 100)]; // [circle1 fill]; //畫弧 //Center:圓心 //radius:半徑 //startAngle:開始角度 0:圓右側 //endAngle:結束角度 //clockwise YES:順時針 NO:逆時針 CGPoint center = CGPointMake(rect.size.width*0.5, rect.size.height*0.5); UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(rect.size.width*0.5, rect.size.height*0.5) radius:50 startAngle:0 endAngle:-M_PI_2 clockwise:NO]; [path addLineToPoint:center]; // [path addLineToPoint:CGPointMake(center.x+50, center.y)]; //上行代碼可以使用封閉路徑 [path closePath]; [path stroke];}/** * 畫矩形 */-(void)drawRect{ CGContextRef contextRef = UIGraphicsGetCurrentContext(); UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(10, 10, 100, 100)]; CGContextAddPath(contextRef, path.CGPath); CGContextStrokePath(contextRef);}/** * 畫圓 */-(void)drawQuadCurve{ CGContextRef textRef = UIGraphicsGetCurrentContext(); UIBezierPath *path = [UIBezierPath bezierPath]; [path moveToPoint:CGPointMake(20, 180)]; [path addQuadCurveToPoint:CGPointMake(200, 180) controlPoint:CGPointMake(110, 10)]; CGContextSetLineWidth(textRef, 10); CGContextAddPath(textRef, path.CGPath); CGContextStrokePath(textRef);}/** * 畫線 */-(void)drawLine{ //1.取得一個跟View相關的上下文 CGContextRef contextRex = UIGraphicsGetCurrentContext(); //2.1 描述路徑 UIBezierPath *bezierPath = [UIBezierPath bezierPath]; //2.2 設定起點 [bezierPath moveToPoint:CGPointMake(10, 100)]; //2.3 設定終點 [bezierPath addLineToPoint:CGPointMake(100, 20)]; //2.4再設定一個終點 [bezierPath addLineToPoint:CGPointMake(100, 100)]; //*設定路徑屬性 - 上下文狀態 //寬度 CGContextSetLineWidth(contextRex, 10); //線的串連樣式 // kCGLineJoinMiter, 預設 // kCGLineJoinRound, 圓弧 // kCGLineJoinBevel 方形 CGContextSetLineJoin(contextRex, kCGLineJoinBevel); //顏色 [[UIColor redColor] setStroke]; //3.把路徑添加到上下文 CGContextAddPath(contextRex, bezierPath.CGPath); //4.把上下文顯示在View上 CGContextStrokePath(contextRex);}
iOS-Quartz2D畫圖