Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CIFilter with UISlider

Tags:

ios

hue

So I have a UISlider that is changing a HUE using CIFilter. Its insanely slow because I'm affecting the base image while the uislider is in use.

Any one have suggestions on how to do this more efficiently?

 // UI SLIDER
-(IBAction)changeSlider:(id)sender {

    [self doHueAdjustFilterWithBaseImage:currentSticker.image 
                            hueAdjust:[(UISlider *)sender value]];

}
//Change HUE
-(void)doHueAdjustFilterWithBaseImage:(UIImage*)baseImage hueAdjust:(CGFloat)hueAdjust {

    CIImage *inputImage = [[CIImage alloc] initWithImage:baseImage];
    CIFilter * controlsFilter = [CIFilter filterWithName:@"CIHueAdjust"];

    [controlsFilter setValue:inputImage forKey:kCIInputImageKey];
    [controlsFilter setValue:[NSNumber numberWithFloat:hueAdjust] forKey:@"inputAngle"];

    //NSLog(@"%@",controlsFilter.attributes);

    CIImage *displayImage = controlsFilter.outputImage;
    CIContext *context = [CIContext contextWithOptions:nil];

    if (displayImage == nil){

        NSLog(@"Display NADA");

    } else {

        NSLog(@"RETURN Image");
        currentSticker.image =  [UIImage imageWithCGImage:[context createCGImage:displayImage fromRect:displayImage.extent]];

    }

    displayImage = nil;
    inputImage = nil;
    controlsFilter = nil;

}
like image 590
Franky Avatar asked Jun 17 '12 00:06

Franky


3 Answers

You can set UISlider's continuous property to NO. So that your changeSlider only gets called when user releaseS the finger. Here is Apple's doc?

like image 80
user523234 Avatar answered Nov 12 '22 02:11

user523234


Your problem here is that you keep redeclaring the context. Put the context as a property and initialize it once in your initializer, then use it over and over and see a massive performance gain.

like image 39
Ser Pounce Avatar answered Nov 12 '22 02:11

Ser Pounce


I guess rather changing the awesome behaviour you want, a frequent update while sliding is good! and it's best for user experience. So Do not change that behaviour but rather work on your algorithm to achieve greater optimisation. Check this previously asked question which has some good tips.

How to implement fast image filters on iOS platform


My go would be, keep updating slider property but try to code something which makes the update when slider hits a number which is a whole decimal(funny term, and its probably wrong too) I mean detect when the slider passes thru 10.000, 20.000, 30.000. Only then update the image rather updating for every single point. Hope this makes sense.

EDIT:

Make your,

Input mage and filter variables as iVar. and check they have been already allocated then reuse it rather then creating every single time.

like image 1
TeaCupApp Avatar answered Nov 12 '22 02:11

TeaCupApp