title: Convert from CIRectangleFeature coordinates to view coordinates
body: <p>I am using the CIDetector class in IOS to find CIRectangleFeatures in my UIImage. Afterwards, I aim to show the cornerPoints drawn into a layer which I then, in turn, add to my UIImageView. Unfortunately, the coordinates given with the CIRectangleFeature are in image space. And, even though I am trying to convert them using the CGContextConvertToUserSpace function, the rectangle drawn is quite off of the actual rectangle in the image.</p>
  
  <p>Here is my code, when an image is taken, workWithTheImage is called:</p>
  
  <pre><code>func analyzeImage(image: UIImage) -&gt; [RectanglePoint]
      {
          guard let ciImage = CIImage(image: image)
              else { return [] }
  
          let context = CIContext(options: nil)
  
          let detector = CIDetector(ofType: CIDetectorTypeRectangle, context: context, options: nil)
  
          let features = detector.featuresInImage(ciImage)
  
          UIGraphicsBeginImageContext(ciImage.extent.size)
          let currentContext = UIGraphicsGetCurrentContext()
  
          var points: [RectanglePoint] = []
  
          for feature in features as! [CIRectangleFeature]
          {
              let topLeft = CGContextConvertPointToUserSpace(currentContext, feature.topLeft)
              let topRight = CGContextConvertPointToUserSpace(currentContext, feature.topRight)
              let bottomRight = CGContextConvertPointToUserSpace(currentContext, feature.bottomRight)
              let bottomLeft = CGContextConvertPointToUserSpace(currentContext, feature.bottomLeft)
  
              let point = RectanglePoint(bottomLeft: bottomLeft, topLeft: topLeft, bottomRight: bottomRight, topRight: topRight)
              points.append(point)
          }
  
          UIGraphicsEndImageContext()
          return points
      }
  
  
  
      func workWithImage(image: UIImage)
      {
          let imageView = UIImageView(frame: view.frame)
          imageView.image = image
  
          let path = UIBezierPath()
          let shapeLayer = CAShapeLayer()
          shapeLayer.frame = imageView.bounds
  
          for i in analyzeImage(image)
          {
              path.moveToPoint(i.topLeft)
              path.addLineToPoint(i.topRight)
              path.addLineToPoint(i.bottomRight)
              path.addLineToPoint(i.bottomLeft)
              path.addLineToPoint(i.topLeft)
          }
  
          shapeLayer.path = path.CGPath
          shapeLayer.fillColor = UIColor.clearColor().CGColor
          shapeLayer.strokeColor = UIColor.redColor().CGColor
  
          imageView.layer.addSublayer(shapeLayer)
      }
  </code></pre>