内存泄漏导致我的GPUImage相机应用崩溃

时间:2014-01-16 12:59:15

标签: ios iphone objective-c image-processing gpuimage

我在尝试拍摄超过1张照片时遇到了一些问题。应用程序变慢,然后由于多个内存警告而崩溃。我正在使用Brad的 GPUImage 库。以下是我使用它的一些方法:

//创建摄影机视图

- (IBAction)photoFromCamera
{
    imageView = [[GPUImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    imageView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:imageView];

    stillCamera = [[GPUImageStillCamera alloc] init];

    stillCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
    //stillCamera.outputImageOrientation = UIInterfaceOrientationLandscapeLeft;

    filter = [[GPUImageFilter alloc] init];
    [filter prepareForImageCapture];
    [stillCamera addTarget:filter];

    [stillCamera addTarget:imageView];

    [filter addTarget:imageView];
    [stillCamera startCameraCapture];

    UIButton * shotButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    shotButton.frame = CGRectMake(137, 361, 46, 30);
    [shotButton setTitle:@"Take!" forState:UIControlStateNormal];
    [shotButton addTarget:self action:@selector(shotAction:) forControlEvents:UIControlEventTouchUpInside];

    [imageView addSubview:shotButton];

    UIButton * cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    cancelButton.frame = CGRectMake(10, 80, 55, 40);
    [cancelButton setTitle:@"cancel" forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];

    [imageView addSubview:cancelButton];
}

//拍摄照片

-(void)shotAction:(UIImagePickerController *)photoPicker{
    [stillCamera capturePhotoAsPNGProcessedUpToFilter:filter withCompletionHandler:^(NSData *processedPNG, NSError *error) {
        self.saveButton.enabled = YES;
        self.filterButton.enabled = YES;
        self.cropButton.enabled = YES;

        originalFirstImage = [UIImage imageWithData:processedPNG];

        [self.selectedImageView setImage:originalFirstImage];
    }];

    [imageView removeFromSuperview];
}

有什么不对吗?我正在使用ARC,有什么东西没有发布吗? 感谢您的评论。

编辑:错误是:因内存压力而终止

1 个答案:

答案 0 :(得分:3)

镜头动作方法有块,并使用self阻止。 Block总是捕获它的环境,意味着它会强烈地引用每一件事。因此它会导致保留周期,这将不允许释放内存。您应该使用弱指针访问self。我会建议你

__weak typeof(self) weakSelf = self;

并在块中使用此weakSelf。

修改

GPUImage库也报告了一些内存泄漏问题,请阅读此forum