微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

应用在 iOS 中使用滑块应用图像效果亮度时崩溃

如何解决应用在 iOS 中使用滑块应用图像效果亮度时崩溃

我正在使用图像视图并根据滑块值对图像视图的图像应用亮度效果。 滑块值范围从 -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,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;
    }

解决方法

如果你想改变 Image 的亮度,检查下面的代码。

注意:您需要在真实设备上对其进行测试,因为它使用 GPU 上下文。在模拟器上它会有延迟。

using CoreGraphics;
using CoreImage;
using Foundation;
using System;
using UIKit;
using OpenGLES;
using System.Threading.Tasks;

namespace App10
{
    public partial class ViewController : UIViewController
    {


        UIImageView imageView;
        CIContext context;
        UIImage originalImage;

        CIColorControls colorCtrls;
        bool isbusy;
        public ViewController (IntPtr handle) : base (handle)
        {
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            originalImage = UIImage.FromBundle("Images.png");

            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("Images.png");
            mainView.Add(imageView);

            UISlider slider = new UISlider();
            slider.Frame = new CGRect(frame.Left,imageView.Frame.Bottom,100);
            slider.MinValue = -100;
            slider.MaxValue = 100;
            slider.ValueChanged += Slider_TouchUpInside; 
            mainView.Add(slider);
            this.Add(mainView);


            colorCtrls = new CIColorControls();
            colorCtrls.InputImage = new CIImage(originalImage);

            // Create the context only once,and re-use it
            var contextOptions = new CIContextOptions();
            contextOptions.UseSoftwareRenderer = false; // gpu vs. cpu
                                                        // On save of the image,create a new context with highqual attributes and re-apply the filter... 
            contextOptions.HighQualityDownsample = false;
            contextOptions.PriorityRequestLow = false; // high queue order it
            contextOptions.CIImageFormat = (int)CIFormat.ARGB8; // use 32bpp,vs. 64|128bpp 
            context = CIContext.FromOptions(contextOptions);


            // Perform any additional setup after loading the view,typically from a nib.
        }

        private async void Slider_TouchUpInside(object sender,EventArgs e)
        {
            if(!isbusy)
            {
                isbusy = true;
                var slider = sender as UISlider;
                imageView.Image = await FilterImageAsync(slider.Value / 100);
                isbusy = false;
            }
        }


      
        async Task<UIImage> FilterImageAsync(float value)
        {  

             var transformImage = new Func<UIImage>(() => {
                    colorCtrls.Brightness = value;
                    CIImage input = new CIImage(originalImage);
                    var output = colorCtrls.OutputImage;
                    var cgImage = context.CreateCGImage(output,input.Extent);
                    var filteredImage = new UIImage(cgImage);
                    return filteredImage;
                });
          
            
            UIImage image = await Task.Run<UIImage>(transformImage);
            return image;
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。