CocoaPods trunk is moving to be read-only. Read more on the blog, there are 18 months to go.

IMOFilterStack 0.1.1

IMOFilterStack 0.1.1

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Mar 2016

Maintained by Ivan Moskalev.



Easily and asynchronously apply chains of CoreImage filters. Pure joy.

- (void)processImage
{
    UIImage *img = [UIImage imageNamed:@"TestImage.jpg"];
    [self.greyscaleAndPixelate processImage:img completion:^(UIImage *result, NSError *error) {
        [self.imageView setImage:result];
    }];
}

- (IMOFilterStack *)greyscaleAndPixelate 
{
    return [IMOFilterStack withFilters:@[ [CIFilter filterWithName:@"CIPhotoEffectNoir"], 
                                          [CIFilter filterWithName:@"CIPixellate"] 
                                          ]];
}

Why use IMOFilterStack?

It is cool. Less code. Reusable. Declarative.

Normally you would have to repeat the following dance to process an UIImage using CoreImage filters:

  • convert to CIImage
  • setup an array of filters
  • iterate through all these filters, applying them to image
  • convert CIImage back to correct UIImage (either CIImage- or CGImage-backed)

IMOFilterStack implements all of these steps. Now all you need to do is to declaratively describe a filter chain. A voila! – simple as that.

Usage

Declare a filter chain like this:

- (IMOFilterStack *)imageBeautifier
{
    return [IMOFilterStack withFilters:@[ [CIFilter filterWithName:@"CIExposureAdjust" keysAndValues:kCIInputEVKey, @(0.10f), nil],
                                          [CIFilter filterWithName:@"CIVibrance" keysAndValues:@"inputAmount", @(0.10f), nil] 
                                          ]];
}
lazy var beautifier: IMOFilterStack = {
    return IMOFilterStack(filters: [
        CIFilter(name: "CIExposureAdjust", withInputParameters: [kCIInputEVKey : NSNumber(double: 0.10)]),
        CIFilter(name: "CIVibrance", withInputParameters: ["inputAmount" : NSNumber(double: 0.10)])
        ])
}()

And then use it like this:

[self.imageBeautifier processImage:photo completion:^(UIImage *result, NSError *error) {
    [self.handler processedPhoto:result originalPhoto:photo];
}];
self.beautifier.processImage(photo) { (result, error) in
    self.handler?.processedPhoto(result, originalPhoto: photo)
}

completion block fires on the main thread. If you want to change that, provide your own instance of NSOperationQueue for callbacks.

You can also provide (or tweak) the NSOperationQueue on which the processing takes place (for example, set QoS).

IMOFilterStack is thread-safe (some thread-safety problems with multiple instances of CIFilter have been reported, I haven't reproduced them yet though). IMOFilterStack tries to do its best, running each processing operation on a deep copy of the original filter array.

Seems to be all for now, folks. Feel free to open issues, fork and send pull requests.

Happy processing!

Installation

Author

Ivan Moskalev, [email protected]

License

IMOFilterStack is available under the MIT license. See the LICENSE file for more info.