Adding Image Over Detected Rectangle
<p>I'm using a <code>CIDetector</code> to detect a rectangle which i want to replace with a rectangular image.</p>

<p>The detection is working fine and i can draw an overlay over it, any idea how can i make the image fit and stick to all corners?</p>

<p>Here's the code: </p>

<pre><code> func drawHighlightOverlayForPoints(image: CIImage, topLeft: CGPoint, topRight: CGPoint,
                                     bottomLeft: CGPoint, bottomRight: CGPoint) -&gt; CIImage {
    var overlay = CIImage(color: CIColor(red: 1.0, green: 0, blue: 0, alpha: 0.5))


    overlay = overlay.imageByCroppingToRect(image.extent)
    overlay = overlay.imageByApplyingFilter("CIPerspectiveTransformWithExtent",
      withInputParameters: [
        "inputExtent": CIVector(CGRect: image.extent),
        "inputTopLeft": CIVector(CGPoint: topLeft),
        "inputTopRight": CIVector(CGPoint: topRight),
        "inputBottomLeft": CIVector(CGPoint: bottomLeft),
        "inputBottomRight": CIVector(CGPoint: bottomRight)
      ])
    return overlay.imageByCompositingOverImage(image)
  }
</code></pre>

<p>If i simply replace the overlay with a image, the image shows up very small and sticks to only one particular corner and doesn't fill the rect. </p>

<pre><code>let testImage = UIImage(named: "card.jpg")

var overlayImage = CIImage(image: testImage!)
</code></pre>

<p><strong>Update:</strong></p>

<p>Using Simons project gives me the result i wanted,though the frame rate drops to less than 1 fps after a while, i do not know whats causing this might have been my implementation ? </p>

<pre><code>  func newCameraImage(cameraCaptureHelper: CameraCaptureHelper, image: CIImage)
    {
        //halftone.setValue(image, forKey: kCIInputImageKey)

        if let rect = detector.featuresInImage(image).first as? CIRectangleFeature {

            perspectiveTransform.setValue(CIVector(CGPoint:rect.topLeft),
                                          forKey: "inputTopLeft")
            perspectiveTransform.setValue(CIVector(CGPoint:rect.topRight),
                                          forKey: "inputTopRight")
            perspectiveTransform.setValue(CIVector(CGPoint:rect.bottomRight),
                                          forKey: "inputBottomRight")
            perspectiveTransform.setValue(CIVector(CGPoint:rect.bottomLeft),
                                          forKey: "inputBottomLeft")


            perspectiveTransform.setValue(addImage,
                                          forKey: kCIInputImageKey)

            composite.setValue(image, forKey: kCIInputBackgroundImageKey)

            composite.setValue(perspectiveTransform.outputImage!, forKey: kCIInputImageKey)

            // let transformFinal = composite.outputImage

            imageView.image = composite.outputImage


        } else {

        imageView.image = image

        }
    }
</code></pre>