DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

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

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
  1. DZone
  2. Coding
  3. Frameworks
  4. Two Handy Swift Extensions: Hermite Splines and Image Resizing

Two Handy Swift Extensions: Hermite Splines and Image Resizing

Simon Gladman user avatar by
Simon Gladman
·
Sep. 22, 14 · Interview
Like (0)
Save
Tweet
Share
3.52K Views

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:

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.


Swift (programming language)

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

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: