title: Adding image to a CAShapeLayer Swift 3
body: <p>I am trying to draw a pie chart with a segment array and an image in the center of each slice , I am have drawn the pie chart , but not able to add an image to the cashapelayer.I refered to the following post <a href="https://stackoverflow.com/questions/29279474/swift-add-image-to-cashapelayer">Swift: Add image to CAShapeLayer</a> , but it did not solve my issue.</p>
  
  <p>This is what I have tried.</p>
  
  <pre><code>override func draw(_ rect: CGRect) {
          //let context = UIGraphicsGetCurrentContext()
          let radius = min(frame.size.width,frame.size.height) * 0.5;
          let viewCenter = CGPoint(x: frame.size.width * 0.5, y: frame.size.height * 0.5)
          let valueCount = segments.reduce(0){ $0 + $1.value}
          var startAngle = -CGFloat(M_PI / 2)
  
          for segment in segments {
             let endAngle = startAngle + CGFloat(M_PI * 2) * (segment.value)/valueCount
  
              /*context?.move(to: CGPoint(x: viewCenter.x, y: viewCenter.y))
              context?.setFillColor(segment.color.cgColor)
              context?.setStrokeColor(UIColor.black.cgColor)
              context?.setLineWidth(3.0)
              context?.addArc(center: viewCenter, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: false)
              context?.fillPath()*/
  
              let pieSliceLayer = CAShapeLayer()
              let slicePath = UIBezierPath(arcCenter: viewCenter, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: false)
              pieSliceLayer.path = slicePath.cgPath
              pieSliceLayer.fillColor = segment.color.cgColor
              self.layer.addSublayer(pieSliceLayer)
  
              let halfAngle = startAngle + (endAngle - startAngle) * 0.5;
              let imagePositionValue = CGFloat(0.67)
              let segmentCenter = CGPoint(x:viewCenter.x+radius*imagePositionValue*cos(halfAngle),y:viewCenter.y+radius*imagePositionValue*sin(halfAngle))
  
              //If image is associated , add the image to the section
              let imageLayer = CAShapeLayer()
              let image = UIImage(named: segment.imageName)
              imageLayer.contents = image?.ciImage
              imageLayer.frame = CGRect(x: segmentCenter.x - 20, y: segmentCenter.y - 20, width: 20, height: 20)
              self.layer.addSublayer(imageLayer)
              startAngle = endAngle
  
          }
      }
  </code></pre>
  
  <p>I tried something similar in objective-C and it works fine ,</p>
  
  <pre><code>CALayer *imageLayer = (CALayer *)[[pieLayer sublayers] objectAtIndex:0];
      //Adding image
      CALayer *layer = [CALayer layer];
      layer.backgroundColor = [[UIColor clearColor] CGColor];
      layer.bounds = CGRectMake(imageLayer.bounds.origin.x +20 ,
                                    imageLayer.bounds.origin.y + 20, 15, 15);
      NSString *imageName = [NSString stringWithString:[[self.goalSettingsModel objectAtIndex:value] valueForKeyPath:@"imageName"]];
      layer.contents =  (id)[UIImage imageNamed:imageName].CGImage;
      [imageLayer addSublayer:layer];
      [imageLayer setNeedsDisplay];
  </code></pre>
  
  <p>I am not sure , if  I have missed any settings for the calayer.</p>