由于内存不足警告我的应用程序不断崩溃,如何以内存有效的方式使用GPUImage?

时间:2014-09-23 11:31:07

标签: ios objective-c gpuimage

我正在尝试用GPUImage创建一个简单的照片编辑器。我有两个问题。一个是当我滑动滑块以提前亮度或对比度时,应用程序会让崩溃。在模拟器上我不会遇到这些问题,但接下来是第二个问题。当控制台上显示内存不足警告时,图像以横向方向转换(我不知道为什么)。谁能指出我正确的方向?我在论坛上,在这里和github上看到很多用户都面临内存问题,所以这可能是库中的内存泄漏?我是iOS开发的新手,所以是的,我希望你们能够详细说明它。附:我正在使用iphone 4s和ios 7

//The Action List
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    GPUImageFilter *selectedFilter;
    //GPUImageBrightnessFilter *brightnessFilter;
    //GPUImageContrastFilter *contrastFilter;
    //GPUImageSaturationFilter *saturationFilter;
    //sliderValue = 0.0;
    bool choiceChecker = NO;
    bool choiceChecker1 = NO;
    bool choicechecker2 = NO;
    //self.stepper.value = 0;

    switch (buttonIndex) {
        case 0:
            selectedFilter = [[GPUImageGrayscaleFilter alloc] init];
            break;
        case 1:
            selectedFilter = [[GPUImageSepiaFilter alloc] init];
            break;
        case 2:
            selectedFilter = [[GPUImageSketchFilter alloc] init];
            break;
        case 3:
            selectedFilter = [[GPUImagePixellateFilter alloc] init];
            break;
        case 4:
            selectedFilter = [[GPUImageColorInvertFilter alloc] init];
            break;
        case 5:
            selectedFilter = [[GPUImageToonFilter alloc] init];
            break;
        case 6:
            selectedFilter = [[GPUImagePinchDistortionFilter alloc] init];
            break;
        case 7:
            choiceChecker1 = YES;
            break;
        case 8:
            choiceChecker = YES;
            break;
        case 9:

            choicechecker2 = YES;
            break;
        case 10:
            selectedFilter = [[GPUImageFilter alloc] init];
            break;
        default:
            break;
    }
    //UIImage *selectedImage =
    UIImage *filteredImage;

    //for brightness
    if(choiceChecker1 == YES)
    {
        self.brightnessSlider.hidden = NO;
        self.contrastSlider.hidden = YES;
        self.saturationSlider.hidden = YES;
        [self.imageView setImage:originalImage];
        NSLog(@"hello");

    }
    //for contrast
    else if(choiceChecker == YES)
    {
        self.contrastSlider.hidden = NO;
        self.brightnessSlider.hidden = YES;
        self.saturationSlider.hidden = YES;
        //[self.imageView setImage:originalImage];

    }
    //for saturation
    else if(choicechecker2 == YES)
    {
        self.saturationSlider.hidden = NO;
        self.brightnessSlider.hidden = YES;
        self.contrastSlider.hidden = YES;
        [self.imageView setImage:originalImage];
    }
    //for filters
    else if (choiceChecker == NO && choiceChecker1 == NO && choicechecker2 == NO)
    {
    filteredImage = [selectedFilter imageByFilteringImage:originalImage];
    [self.imageView setImage:filteredImage];
    originalImage = filteredImage;
    }
}

//  Edited action listener for brightness slider (testView is a GPUImageView)
-(IBAction)BrightnessSlid:(UISlider *)slider {
     sliderValue = self.brightnessSlider.value;
     //self.contrastSlider.hidden = YES;
     fx_image = [[GPUImagePicture alloc] initWithImage:originalImage];
     [brightnessFilter setBrightness:sliderValue];

      [fx_image addTarget:brightnessFilter];
      [brightnessFilter useNextFrameForImageCapture];
     [brightnessFilter forceProcessingAtSize:CGSizeMake(320.0, 519.0)];
    [fx_image processImage];


     [fx_image addTarget:testView];
     [self.imageView addSubview:testView];
     final_image = [brightnessFilter imageFromCurrentFramebuffer];
     //originalImage = final_image;
      }

1 个答案:

答案 0 :(得分:3)

跳出来的第一件事是你的滑块方法,效率非常低。您每次触发都会创建一个新的GPUImagePicture并生成UIImage。你不想这样做。

相反,请将您的GPUImagePicture实例化一次,将其定位到适当的过滤器一次,然后根据需要在滑块中调整值。将输出定位到GPUImageView而不是UIImageView。这样可以避免在滑块的每次移动时浪费图像创建和破坏,并且可以防止在任何一端通过Core Graphics进行昂贵的访问。就其本身而言,这将为您节省大量内存并显着提高滑块预览性能。

您可能还想考虑以较低的分辨率处理图像,以匹配输出视图的分辨率。无需处理您将看不到的像素。这可以使用过滤器上的-forceProcessingAtSize:来完成。

您需要记住在将它们换出时断开GPUImagePicture和GPUImageView的过滤器,并在每次要更新预览图像时在GPUImagePicture上使用-processImage