我正在使用图像视图并根据滑块值将亮度效果应用于图像视图的图像。滑块值范围从 -100 到 +100。在连续移动滑块时,应用程序会关闭。我什至尝试过处理上下文但同样的问题存在。请参考以下代码并帮助我解决。
UIImageView imageView;
private float brightnessAmount;
private bool isBrightnessApplied;
private ImageEffect effects;
private CIContext filterContext;
UIImage originalImage;
public override void ViewDidLoad()
{
base.ViewDidLoad();
effects = ImageEffect.Brightness;
originalImage = UIImage.FromBundle("image.jpg");
var frame = this.View.Frame;
UIView mainView = new UIView();
mainView.Frame = frame;
imageView = new UIImageView();
imageView.Frame = new CGRect(frame.Left, frame.Top, frame.Width, frame.Height - 100);
imageView.Image = UIImage.FromBundle("image.jpg");
mainView.Add(imageView);
UISlider slider = new UISlider();
slider.Frame = new CGRect(frame.Left, imageView.Frame.Bottom, frame.Width, 100);
slider.MinValue = -100;
slider.MaxValue = 100;
slider.ValueChanged += Slider_ValueChanged;
mainView.Add(slider);
this.Add(mainView);
}
private void Slider_ValueChanged(object sender, EventArgs e)
{
var amount = (sender as UISlider).Value;
amount = amount > 100 ? 100 : amount < -100 ? -100 : amount;
amount = amount / 100;
CIImage currentImage = GetFilteredCIImage(effects);
CIColorControls colorCtrls = new CIColorControls() { Image = currentImage };
if (effects == ImageEffect.Brightness)
{
colorCtrls.Brightness = amount;
brightnessAmount = amount;
isBrightnessApplied = true;
}
if (filterContext == null )
filterContext = CIContext.FromOptions(null);
var outputImage = colorCtrls.OutputImage;
var result = filterContext.CreateCGImage(outputImage, outputImage.Extent);
filterContext.ClearCaches();
imageView.Image = UIImage.FromImage(result);
result.Dispose();
result = null;
outputImage.Dispose();
outputImage = null;
}
private CIImage GetFilteredCIImage(ImageEffect effect)
{
CIImage currentImage = CIImage.FromCGImage(originalImage.CGImage);
if (effect == ImageEffect.None) return currentImage;
if ( isBrightnessApplied)
{
CIColorControls colorCtrls = new CIColorControls() { Image = currentImage };
colorCtrls.Brightness = brightnessAmount;
currentImage = colorCtrls.OutputImage;
colorCtrls.Dispose();
}
return currentImage;
}