2016-02-29 6 views
1

編輯:這裏還有一些其他問題,但它們都可以幫助您在UIView中繪製圓或圓弧。我需要一個也作爲UIImage而不是UIView的扇區。iOS以編程方式創建給定角度和半徑的圓形扇區的UIImage

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; 
} 
+1

http://stackoverflow.com/questions/354610/create-a-colored-bubble-circle-programmatically-in-objectivec-and-cocoa – sandy

+0

這個問題是來自不同礦。它畫了一個圓圈而不是一個扇形。此外,它在uiview中繪製,我需要一個UIImage –

回答

0

你的方法工作正常。但是,我覺得你的輸入對你的功能是錯誤的。

// 1 - Test 

    UIImageView *sample = [[UIImageView alloc] initWithFrame:CGRectMake(20.0f, 122.0f, 50.0f,50.0f)]; 
    sample.image = [self getSegmentImageOfRadius:50 andAngleRadians:50]; 

    [self.view addSubview:sample]; 

    // Add this funciton to your view controller 
    -(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); // Fill color. 
     [sector fill]; 
     UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); // Create image form curent context. 
     UIGraphicsEndImageContext(); 
     return image; 
    } 

輸出:

Output above code

+0

對不起,這是行不通的。它以「50」爲角度繪製了一個奇怪的外觀思路 –

+0

您是否按照上述步驟行事? –

+0

是的,唯一不同的是「angleRadius」通過了50。它沒有工作 –

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]; 
+0

我很抱歉,但這似乎也給我一個奇怪的壓潰扇區:( –

+0

@PranoyC我編輯了我的答案,檢查出來 –

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

+0

對不起,如我所說,我需要它作爲UIImage而不是UIView/UIImageView –

+0

嘗試上面的方法,它會爲您返回UIImage。 –

+0

您是否嘗試過**編輯**答案? –