iOS以编程方式创建给定角度和半径的圆扇区的UIImage

时间:2016-02-29 05:58:00

标签: ios objective-c uiimage quartz-core

编辑:关于SO还有一些其他问题,但它们都可以帮助你在UIView中画一个圆圈或一个圆弧。我需要一个也是UIImage而不是UIView的扇区。

iOS以编程方式创建圆扇区的UIImage,给定角度和半径

我正在尝试根据角度和半径创建圆扇区的UIImage。 最终结果应该是此图像的单色版本。我不需要此图像具有的渐变。单色就好了。

我需要创建许多具有不同颜色的细分。

enter image description here

这是我到目前为止所尝试的内容,但最终看起来像这样我不知道:

enter image description here

致电:

[self getSegmentImageOfRadius:177.5 andAngleRadians:0.63]

方法:

-(UIImage*)getSegmentImageOfRadius:(CGFloat)radius andAngleRadians:(CGFloat)angleRadians{

    UIBezierPath *sector = [UIBezierPath
                            bezierPathWithArcCenter:CGPointMake(0, 0) radius:radius startAngle:0 endAngle:angleRadians clockwise:YES];
    UIGraphicsBeginImageContext(CGSizeMake(radius,radius));
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
    [sector fill];
    UIImage *mage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return mage;
}

3 个答案:

答案 0 :(得分:2)

试试这个..

创建宽度接近190的imageView(根据您的要求)

#define pi 3.14159265359
#define DEGREES_TO_RADIANS(degrees) ((pi * degrees) / 180)

在viewDidLoad

UIImage *image = [self getSegmentImageOfRadius:_imageView.frame.size.width/2 startAngle:160 andEndAngle:200];
_imageView.image = image;

更改方法如下

-(UIImage*)getSegmentImageOfRadius:(CGFloat)radius startAngle:(CGFloat)startAngle andEndAngle:(CGFloat)endAngle
{
    CGPoint center = CGPointMake(_imageView.frame.size.width/2, _imageView.frame.size.height/2);
    UIBezierPath *path = [UIBezierPath bezierPath];
   [path moveToPoint:center];
   [path addArcWithCenter:center radius:radius startAngle:DEGREES_TO_RADIANS(startAngle) endAngle:DEGREES_TO_RADIANS(endAngle) clockwise:YES];
   [path closePath];

   CAShapeLayer *layer = [CAShapeLayer layer];
   layer.frame = _imageView.bounds;
   layer.path = path.CGPath;
   layer.fillColor = [UIColor redColor].CGColor;
   UIGraphicsBeginImageContextWithOptions(layer.frame.size, NO, 0);

   [layer renderInContext:UIGraphicsGetCurrentContext()];
   UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();

   UIGraphicsEndImageContext();

   return outputImage;
}

如果您不需要作为图像,可以在视图的图层中添加形状,如下所示

CGPoint center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:center];
[path addArcWithCenter:center radius:radius startAngle:DEGREES_TO_RADIANS(startAngle) endAngle:DEGREES_TO_RADIANS(endAngle) clockwise:YES];
[path closePath];

CAShapeLayer *layer = [CAShapeLayer layer];    
layer.path = path.CGPath;
layer.fillColor = [UIColor redColor].CGColor;
[self.view.layer addSublayer:layer];

答案 1 :(得分:1)

我创建了半径,起始角度和结束角度的圆形视图。

      240 
       |
180 ---|--- 0 - Start Angle point in my code.
       |
       90

       #define DEGREES_TO_RADIANS(d) ((d) * 0.0174532925199432958f)


       // Sample image view.

           UIImageView *circleView = [[UIImageView alloc] initWithFrame:CGRectMake(50.0f, 382.0f, 120.0f,120.0f)];
            circleView.image = [self getSegmentImageOfRadius:30 startAngle:90 andEndAngle:180 withFrame:circleView.frame.size];
            [self.view addSubview:circleView];


        -(UIImage*)getSegmentImageOfRadius:(CGFloat)radius startAngle:(CGFloat)startAngle andEndAngle:(CGFloat)endAngle withFrame :(CGSize) size {

            // Create View with frame of radius
            UIView *circleView = [[UIView alloc] initWithFrame:CGRectMake(radius, radius, radius*2, radius*2)];

            // Create new object for UIBezierPath
            UIBezierPath *bezierPath = [UIBezierPath bezierPath];

            // Find  the center point of the view.
            CGPoint center = CGPointMake(CGRectGetMidX(circleView.frame), CGRectGetMidY(circleView.frame));
            // Add the arc with given properties.
            [bezierPath addArcWithCenter:center radius:radius startAngle:DEGREES_TO_RADIANS(startAngle) endAngle: DEGREES_TO_RADIANS(endAngle) clockwise:YES];

            // Add the shape layer.
            CAShapeLayer *progressLayer = [[CAShapeLayer alloc] init];
            [progressLayer setPath:bezierPath.CGPath];
            [progressLayer setStrokeColor:[UIColor blueColor].CGColor];
            [progressLayer setFillColor:[UIColor clearColor].CGColor];
            [progressLayer setLineWidth:circleView.frame.size.width];
            [circleView.layer addSublayer:progressLayer];

            // Convert and render UIView into UIImage.
            UIGraphicsBeginImageContext(size);
            CGContextRef context = UIGraphicsGetCurrentContext();
            [circleView.layer renderInContext:context];
            UIImage *saveImage;

            saveImage = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            return saveImage;
        }

参考:2DDrawing

答案 2 :(得分:0)

你的方法工作正常。但是,我觉得你的输入对你的功能有误。

ORDER BY RAND() LIMIT 1;

输出:

Output above code