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;
}
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?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With