Two Handy Swift Extensions: Hermite Splines and Image Resizing
Join the DZone community and get the full member experience.
Join For 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:
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.
Published at DZone with permission of Simon Gladman, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Using Render Log Streams to Log to Papertrail
-
How To Use Geo-Partitioning to Comply With Data Regulations and Deliver Low Latency Globally
-
Automating the Migration From JS to TS for the ZK Framework
-
Replacing Apache Hive, Elasticsearch, and PostgreSQL With Apache Doris
Comments