UIImage *img=[UIImage imageNamed:@"icon_service_sharp_blue"];
// 這個(gè)要說(shuō)下,不知道你們用的時(shí)候,是否有毛邊??我的是有,所以不用這個(gè),感興趣的朋友可以驗(yàn)證我說(shuō)的
// UIGraphicsBeginImageContext(img.size );
// ****應(yīng)該用這個(gè)方式開(kāi)啟圖片上下文,因?yàn)椴煌謾C(jī)需要開(kāi)啟不同比例的圖片上下文,1x 2x 3x的 ****
UIGraphicsBeginImageContextWithOptions(rect.size, NO, image.scale);
// 設(shè)置背景顏色
[[UIColor clearColor]set];
UIRectFill([self bounds]);
// 獲取上下文
CGContextRef ctr =UIGraphicsGetCurrentContext();
// 第一種方式
CGContextBeginPath(ctr);
CGPoint sPoints[3];//坐標(biāo)點(diǎn)
sPoints[0] =CGPointMake(0 , 0);//坐標(biāo)1
sPoints[1] =CGPointMake(img.size.width , img.size.height * 0.5);//坐標(biāo)2
sPoints[2] =CGPointMake(0, img.size.height);//坐標(biāo)3
CGContextAddLines(ctr, sPoints, 3);//添加線
CGContextClosePath(ctr);//封起來(lái)
[[顏色值] setFill];
CGContextDrawPath(ctr, kCGPathFillStroke); //根據(jù)坐標(biāo)繪制路徑
// 第二種方式
// CGContextMoveToPoint(ctr, 0 , 0);
// CGContextAddLineToPoint(ctr, 10 , 7);
// CGContextAddLineToPoint(ctr, 0, 14);
// CGContextClosePath(ctr);
// [[顏色值] setFill];
// CGContextDrawPath(ctr, kCGPathFillStroke);
// 獲取圖片
UIImage *newImg=UIGraphicsGetImageFromCurrentImageContext();
// 結(jié)束上下文
UIGraphicsEndImageContext();
}
這里就要說(shuō)下了,在使用第二種方式的時(shí)候不會(huì)出現(xiàn)三角形效果,但是不是代碼問(wèn)題,是因?yàn)樽鴺?biāo)問(wèn)題,應(yīng)該用第一種方式的坐標(biāo)來(lái)就能出現(xiàn)三角形。
最后推薦個(gè)地址:http://blog.csdn.net/chocolateloveme/article/details/17246887
IOS繪制圓,直線,弧線,矩形,扇形,三角形,貝塞爾等圖形