本地声明' imgView'隐藏实例变量(app崩溃)

时间:2014-05-13 13:04:02

标签: ios objective-c warnings

如果我调试我的应用程序。应用程序在这行代码中崩溃,我收到此警告:

警告:

Local declaration of 'imgView' hides instance variable

代码行:

[brightnessFilter forceProcessingAtSize:imgView.sizeInPixels]; 

代码块:

- (IBAction)sliderBrightness:(id)sender {

    UIImage *inputImage = imgView.image;

    sourcePicture = [[GPUImagePicture alloc] initWithImage:inputImage smoothlyScaleOutput:YES];
    brightnessFilter = [[GPUImageBrightnessFilter alloc] init];

    GPUImageView *imgView = (GPUImageView *)self.view;
    [brightnessFilter forceProcessingAtSize:imgView.sizeInPixels]; 

    [sourcePicture addTarget:brightnessFilter];
    [brightnessFilter addTarget:imgView];

    [sourcePicture processImage];
}

有人可以帮我解决问题吗?

3 个答案:

答案 0 :(得分:1)

安德烈给了你基础知识。非常具体。

这是您当前的代码:

- (IBAction)sliderBrightness:(id)sender {

    UIImage *inputImage = imgView.image; // <--- This line refers to an instance var

    sourcePicture = [[GPUImagePicture alloc] initWithImage:inputImage smoothlyScaleOutput:YES];
    brightnessFilter = [[GPUImageBrightnessFilter alloc] init];

    GPUImageView *imgView = (GPUImageView *)self.view; <--- This line defines a local
    [brightnessFilter forceProcessingAtSize:imgView.sizeInPixels]; 

    [sourcePicture addTarget:brightnessFilter];
    [brightnessFilter addTarget:imgView];

    [sourcePicture processImage];
}

查看我添加的评论,以“&lt; --- This line”开头。第一个标记您对实例变量imgView的引用。

第二个注释标记了一个用同一名称定义不同的局部变量imgView的地方。

如果您打算使用其他本地变量,请将该名称更改为其他名称,例如

    GPUImageView *theGPUimgView = (GPUImageView *)self.view; 

如果您打算更改实例变量,那么将其作为赋值,并删除变量声明:

   imgView = (GPUImageView *)self.view;

我猜你想要第二种情况,你改变实例变量的内容,但我不确定。这是你的代码。

答案 1 :(得分:0)

替换此代码:

GPUImageView *imgView = (GPUImageView *)self.view;
[brightnessFilter forceProcessingAtSize:imgView.sizeInPixels]; 

[sourcePicture addTarget:brightnessFilter];
[brightnessFilter addTarget:imgView];

使用此代码:

GPUImageView *imgView2 = (GPUImageView *)self.view;
[brightnessFilter forceProcessingAtSize:imgView2.sizeInPixels]; 

[sourcePicture addTarget:brightnessFilter];
[brightnessFilter addTarget:imgView2];

您有两个名为imgView的变量,它会向您发出警告,但没有理由导致崩溃。崩溃是因为您提供的代码之外的其他原因。

答案 2 :(得分:0)

你已经声明了两个名为imgView的变量(Local和Instance)。将imgView的GPUImageView的局部变量实例改为其他名称,例如gpuimgView

- (IBAction)sliderBrightness:(id)sender {

    UIImage *inputImage = imgView.image;

    sourcePicture = [[GPUImagePicture alloc] initWithImage:inputImage smoothlyScaleOutput:YES];
    brightnessFilter = [[GPUImageBrightnessFilter alloc] init];

    GPUImageView *gpuimgView = (GPUImageView *)self.view;//changed to gpuimgView
    [brightnessFilter forceProcessingAtSize:gpuimgView.sizeInPixels]; 

    [sourcePicture addTarget:brightnessFilter];
    [brightnessFilter addTarget:gpuimgView];

    [sourcePicture processImage];
}

希望它会帮助你......