How do I crop a UIImage in Swift?
<p>I want to write a function in Swift that takes an image and crops out everything except a thin horizontal like in the middle. I don't want to preserve the aspect ratio.</p>

<p>This is what I have so far but it doesn't work the way I want it to. I want to only preserve the pixels from y=276 to y=299.</p>

<pre><code>func cropImageToBars(image: UIImage) -&gt; UIImage {

  let rect = CGRectMake(0, 200, image.size.width, 23)

  UIGraphicsBeginImageContextWithOptions(rect.size, false, 1.0)
  image.drawInRect(rect)
  let newImage = UIGraphicsGetImageFromCurrentImageContext()
  UIGraphicsEndImageContext()

  return newImage
</code></pre>

<p>}</p>