Quartz2D知識點聚合
基本
//畫圖片UIImage *image = [UIImage imageNamed:@"阿貍頭像"];[image drawInRect:rect];//字體NSString *title = @"標題";NSMutableDictionary *atr = [NSMutableDictionary dictionary];atr[NSFontAttributeName] = [UIFont systemFontOfSize:15];// atr[NSForeground?ColorAttributeName] = [UIColor greenColor];[title drawInRect:CGRectMake(120, 20, 100, 20) withAttributes:atr];//橢圓UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 20, 70, 130)];[path stroke];//方形UIBezierPath *path1 = [UIBezierPath bezierPathWithRect:CGRectMake(10, 200, 10, 50)];[path1 stroke];//圓角方形UIBezierPath *path2 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(100, 30, 100, 100) cornerRadius:10];[path2 stroke];//一個角圓角UIBezierPath *path3 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(210, 90, 80, 70) byRoundingCorners:UIRectCornerTopRight cornerRadii:CGSizeMake(20, 30)];[path3 stroke];//圓弧UIBezierPath *path4 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(130, 230) radius:70 startAngle:0 endAngle:M_PI clockwise:YES];[path4 stroke];
// 1.獲得當前上下文CGContextRef ctx = UIGraphicsGetCurrentContext();// 2.拼接路徑UIBezierPath *path = [UIBezierPath bezierPath];[path moveToPoint:CGPointMake(10, 20)];[path addQuadCurveToPoint:CGPointMake(200, 80) controlPoint:CGPointMake(100, 200)];// 3. 添加路徑到上下文CGContextAddPath(ctx, path.CGPath);// 4.渲染上下文CGContextStrokePath(ctx);
變換
//變換//平移
// CGContextTranslateCTM(ctx, 10, 20);//旋轉CGContextRotateCTM(ctx, M_PI_4);//縮放CGContextScaleCTM(ctx, 1.2, 1.2);
上下文棧
// 1.獲得當前上下文CGContextRef ctx = UIGraphicsGetCurrentContext();// 2.拼接路徑UIBezierPath *path = [UIBezierPath bezierPath];[path moveToPoint:CGPointMake(10, 20)];[path addQuadCurveToPoint:CGPointMake(200, 80) controlPoint:CGPointMake(100, 200)];// 3. 添加路徑到上下文CGContextAddPath(ctx, path.CGPath);//保存上下文CGContextSaveGState(ctx);//設置上下文狀態CGContextSetLineWidth(ctx, 10);[[UIColor redColor] set];// 4.渲染上下文CGContextStrokePath(ctx);// 2.拼接路徑path = [UIBezierPath bezierPath];[path moveToPoint:CGPointMake(100, 80)];[path addLineToPoint:CGPointMake(200, 200)];// 3. 添加路徑到上下文CGContextAddPath(ctx, path.CGPath);//還原上下文CGContextRestoreGState(ctx);//設置上下文狀態CGContextSetLineWidth(ctx, 5);[[UIColor blueColor] set];// 4.渲染上下文CGContextStrokePath(ctx);
生成圖片
UIImage *image = [UIImage imageNamed:@"小黃人"];UIGraphicsBeginImageContextWithOptions(image.size, YES, 0);[image drawAtPoint:CGPointZero];NSString *str = @"小黃人";[str drawAtPoint:CGPointZero withAttributes:nil];image = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();
截圖
//開啟圖片上下文UIGraphicsBeginImageContext(view.frame.size);//獲得當前上下文CGContextRef ctx = UIGraphicsGetCurrentContext();//給定裁減區域-----//渲染圖片[view.layer renderInContext:ctx];//從當前上下文得到一張圖片UIImage *image = UIGraphicsGetImageFromCurrentImageContext();//關閉圖片上下文UIGraphicsEndImageContext();return image;
擦除
UITouch *touch = [touches anyObject];CGPoint point = [touch locationInView:self.imageView];//開啟上下文UIGraphicsBeginImageContextWithOptions(self.imageView.frame.size, NO, 0);//獲得當前上下文CGContextRef ctx = UIGraphicsGetCurrentContext();//渲染到上下文[self.imageView.layer renderInContext:ctx];//獲取擦除區域CGRect rect = CGRectMake(point.x - 10, point.y - 10, 20, 20);// 擦除上下文的內容CGContextClearRect(ctx, rect);// 生成圖片UIImage *image = UIGraphicsGetImageFromCurrentImageContext();_imageView.image = image;// 關閉上下文UIGraphicsEndImageContext();