I have a slider with value. When I change the slider I want the image in imageview to become gray scaled based on the value set in slider.

有帮助吗?

解决方案

Firstly you would need to set the image as a property in the view controller, to say myImage for this example.

You also need to create a value changed action for the slider.

Inside the UISlider value changed method, you could use the following:

float sliderValue = VALUE OF SLIDER BETWEEN 0.0 AND 1.0

self.myImage = [UIImage imageWithCGImage:[CIFilter filterWithName:@"CIColorMonochrome" keysAndValues:kCIInputImageKey, beginImage, @"inputIntensity", [NSNumber numberWithFloat:sliderValue], @"inputColor", [[CIColor alloc] initWithColor:[UIColor whiteColor]], nil].outputImage];

This changes the level of monochrome depending on the slider value.

Which uses the CIFilter class: https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/QuartzCoreFramework/Classes/CIFilter_Class/Reference/Reference.html#//apple_ref/occ/clm/CIFilter/filterWithName:keysAndValues:

And the monochrome filter: https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CoreImageFilterReference/Reference/reference.html#//apple_ref/doc/filter/ci/CIColorMonochrome

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top