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