CGContextRef画三角形,拒绝毛边

作者: FMG | 来源:发表于2016-01-06 00:11 被阅读377次
    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绘制圆,直线,弧线,矩形,扇形,三角形,贝塞尔等图形

    相关文章

      网友评论

        本文标题:CGContextRef画三角形,拒绝毛边

        本文链接:https://www.haomeiwen.com/subject/wzozhttx.html