Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.


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))
            }
     
            path.interpolatePointsWithHermite(interpolationPoints)
       
            CGContextSetLineJoin(ctx, kCGLineJoinRound)
            CGContextAddPath(ctx, path.CGPath)
            CGContextSetStrokeColorWithColor(ctx, UIColor.blueColor().CGColor)
            CGContextSetLineWidth(ctx, 6)
            CGContextStrokePath(ctx)
        }

    }

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)
        
        UIGraphicsBeginImageContext(newSize)
        
        self.drawInRect(CGRect(x: 0, y: 0, width: newWidth, height: newHeight))
        
        let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
        
        UIGraphicsEndImageContext();
        
        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.


Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.

Topics:

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

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

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

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

{{ parent.tldr }}

{{ parent.urlSource.name }}