Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I process an image in Swift without using UIImage?

I'm building a Kitura web application, and I need the ability to process user uploaded images.

I have an instance of Foundation's Data, and I'd like to do the following without using any Cocoa libraries/frameworks (other than Foundation):

  • Verify if Data is a valid image file
  • Convert image to .png
  • Check the resolution of the image
  • Resize the image file to a specific width, with height following proportion
  • Save to local file system

How would I do this? I'm fine with using third party libraries / frameworks as long as they work on Linux.

like image 618
Berry Avatar asked Jan 27 '17 08:01

Berry


People also ask

How do I download an image from the server in Swift?

Open ViewController. swift and create an outlet with name imageView of type UIImageView! , an implicitly unwrapped optional. The idea is simple. The view controller downloads an image from a URL and displays it in its image view.

What is Uiimage in Swift?

An object that manages image data in your app.


1 Answers

You should use SwiftGD - a wrapper for libgd. It supports all the operations you require: https://github.com/twostraws/SwiftGD

The only thing it doesn't support directly is reading an image from an in-memory Data instance. You'll need to write it to a temporary file and instantiate from there.

I can't vouch for the Swift wrapper itself, which seems to be brand new, but I've used libgd on other platforms and I haven't had any problems.

Another interesting one that I came across is: https://github.com/BradLarson/GPUImage2

I've used the original GPUImage and it is awesome. This new version supports Linux, but it doesn't sound like it's very polished on that platform yet. It also may require a GPU, which could definitely be a problem depending on your end goal.

In the short-term, SwiftGD seems like your best bet.

like image 131
Dave Weston Avatar answered Sep 22 '22 21:09

Dave Weston