Over a million developers have joined DZone.

Two Handy Swift Extensions: Hermite Splines and Image Resizing

DZone's Guide to

Two Handy Swift Extensions: Hermite Splines and Image Resizing

· Java Zone
Free Resource

Are you joining the containers revolution? Start leveraging container management using Platform9's ultimate guide to Kubernetes deployment.

As part of my Swift Image Tone Curve Editor, I wanted to draw a smooth spline passing through a number of points. There's nothing 'out of the box' in Swift to do this - the standard UIBezierPath can draw cubic and quadratic Beziers, but it takes a little work to set the control points to make a nice continuous curve.

There are two common solutions: Catmull Rom and Hermite. Luckily, I found this great article discussing implementing both in Objective-C and even luckier, it includes the source code. So, it didn't take long for me to port that code to Swift and implement it as an extension to UIBezierPath.

To use my extension, simply create an instance of UIBezierPath, create an array of CGPoint instances - which are the points you want your curve to pass through - and call interpolatePointsWithHermite() with the array. So, now the overridden drawInContext() method in my tone curve editor's ToneCurveEditorCurveLayer class looks like this:

override func drawInContext(ctx: CGContext!)
        if let curveValues = toneCurveEditor?.curveValues
            var path = UIBezierPath()
            let margin = 20
            let thumbRadius = 15
            let widgetWidth = Int(frame.width)
            let widgetHeight = Int(frame.height) - margin - margin - thumbRadius - thumbRadius

            var interpolationPoints : [CGPoint] = [CGPoint]()
            for (i: Int, value: Double) in enumerate(curveValues)
                let pathPointX = i * (widgetWidth / curveValues.count) + (widgetWidth / curveValues.count / 2)
                let pathPointY = thumbRadius + margin + widgetHeight - Int(Double(widgetHeight) * value)
                interpolationPoints.append(CGPoint(x: pathPointX,y: pathPointY))
            CGContextSetLineJoin(ctx, kCGLineJoinRound)
            CGContextAddPath(ctx, path.CGPath)
            CGContextSetStrokeColorWithColor(ctx, UIColor.blueColor().CGColor)
            CGContextSetLineWidth(ctx, 6)


My second useful extension (and one I wrote myself this time!) is on UIImage and resizes images. I needed to do this because applying filters on huge images can be painfully slow and eats up memory. There's no need to apply filters to the full size image during editing, so resizing allows my little application to use a proxy.

This extension accepts a single argument which is the side of a bounding square. The returned image is properly scaled to fit within that box:

extension UIImage
    func resizeToBoundingSquare(#boundingSquareSideLength : CGFloat) -> UIImage
        let imgScale = self.size.width > self.size.height ? boundingSquareSideLength / self.size.width : boundingSquareSideLength / self.size.height
        let newWidth = self.size.width * imgScale
        let newHeight = self.size.height * imgScale
        let newSize = CGSize(width: newWidth, height: newHeight)
        self.drawInRect(CGRect(x: 0, y: 0, width: newWidth, height: newHeight))
        let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
        return resizedImage


...the function returns a new image and can be invoked like this:

loadedImage = rawImage.resizeToBoundingSquare(boundingSquareSideLength: 1024)

This populates loadedImage with a copy of rawImage with a maximum size of 1024 pixels.
Both extensions are available in my GitHub repository. The extension to UIBezierPath is here and the extension toUIImage is here.

Using Containers? Read our Kubernetes Comparison eBook to learn the positives and negatives of Kubernetes, Mesos, Docker Swarm and EC2 Container Services.


Published at DZone with permission of Simon Gladman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}