Avsession提供“记忆警告”

时间:2014-02-12 06:43:22

标签: ios

我正在使用Avsession来捕捉图像。 当我捕获第一个图像时,我将重定向用户对其应用效果,然后返回到我的捕获图像屏幕,我将在凸轮捕获屏幕上预览图像。然后,我将允许用户捕获3个以上的图像,我将预览所有图像凸轮捕获屏幕。当我拍摄第三张照片时,我会收到记忆警告。

捕获每张图片后,我将裁剪图片以调整大小。

我在viewdidload中编写了以下代码

AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch] && [device hasFlash]){
    [device lockForConfiguration:nil];
    [device setTorchMode:cameraFlashMode];
    [device unlockForConfiguration];
    [self.btnFlashLight setHidden:NO];
}
else
    [self.btnFlashLight setHidden:YES];

cameraMode = AVCaptureDevicePositionBack;
firstImage.layer.borderWidth = 2.0f;
firstImage.layer.borderColor = [UIColor grayColor].CGColor;
secondImage.layer.borderWidth = 2.0f;
secondImage.layer.borderColor = [UIColor grayColor].CGColor;
thirdImage.layer.borderWidth = 2.0f;
thirdImage.layer.borderColor = [UIColor grayColor].CGColor;
forthImage.layer.borderWidth = 2.0f;
forthImage.layer.borderColor = [UIColor grayColor].CGColor;
if ([capturedImages count] == 0)
    [self.btnNext setEnabled:NO];

if ([self captureManager] == nil) {
    CamCaptureManager *manager = [[CamCaptureManager alloc] init];
    [self setCaptureManager:manager];

    [[self captureManager] setDelegate:self];

    if ([[self captureManager] setupSession]) {
        // Create video preview layer and add it to the UI
        newCaptureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:[[self captureManager] session]];
        UIView *view = [self videoPreviewView];
        CALayer *viewLayer = [view layer];
        [viewLayer setMasksToBounds:YES];

        CGRect bounds = [view bounds];
        [newCaptureVideoPreviewLayer setFrame:bounds];
        [newCaptureVideoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
        [viewLayer insertSublayer:newCaptureVideoPreviewLayer below:[[viewLayer sublayers] objectAtIndex:0]];

        UIBezierPath *fullScreenPath = [UIBezierPath bezierPathWithRect:view.bounds];
        CAShapeLayer *maskLayer = [CAShapeLayer layer];
        [fullScreenPath appendPath:[UIBezierPath bezierPathWithRoundedRect:[GeneralDeclaration generalDeclaration].cameraMaskFrame byRoundingCorners:UIRectCornerAllCorners                                                                 cornerRadii:CGSizeMake(0.0f, 0.0f)]];
        maskLayer.fillRule = kCAFillRuleEvenOdd;
        maskLayer.path = fullScreenPath.CGPath;
        maskLayer.fillColor = [UIColor blackColor].CGColor;
        maskLayer.opacity = 0.6f;
        maskLayer.lineWidth = 1.0f;
        maskLayer.strokeColor = [UIColor whiteColor].CGColor;
        [viewLayer insertSublayer:maskLayer above:[[viewLayer sublayers] objectAtIndex:0]];

        [self setCaptureVideoPreviewLayer:newCaptureVideoPreviewLayer];
        [[self captureManager] session ].sessionPreset = AVCaptureSessionPresetPhoto;
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            [[[self captureManager] session] startRunning];
        });
    }
}

我写过以下代码来处理捕获的图片:

[[self captureManager] captureStillImage];
     UIView *flashView = [[UIView alloc] initWithFrame:[[self videoPreviewView] frame]];
    [flashView setBackgroundColor:[UIColor whiteColor]];
    [[[self view] window] addSubview:flashView];

    [UIView animateWithDuration:.4f
                     animations:^{
                         [flashView setAlpha:0.f];
                     }
                     completion:^(BOOL finished){
                         [flashView removeFromSuperview];
                     }
     ];

    firstImage.image = nil;
    secondImage.image = nil;
    thirdImage.image = nil;
    forthImage.image = nil;

    //firstImage.userInteractionEnabled = YES;
    secondImage.userInteractionEnabled = YES;
    thirdImage.userInteractionEnabled = YES;
    forthImage.userInteractionEnabled = YES;

    for (UIImageView *subview in [firstImage subviews]) {
        [subview removeFromSuperview];
    }
    for (UIImageView *subview in [secondImage subviews]) {
        [subview removeFromSuperview];
    }
    for (UIImageView *subview in [thirdImage subviews]) {
        [subview removeFromSuperview];
    }
    for (UIImageView *subview in [forthImage subviews]) {
        [subview removeFromSuperview];
    }

for(int i = 0; i < [capturedImages count]; i++)
{
    UIImage *removeImage = [UIImage imageNamed:@"remove_image.png"];
    UIImageView *removeImageView = [[UIImageView alloc] initWithImage:removeImage];
    removeImageView.tag = i;
    [removeImageView setCenter:CGPointMake(firstImage.bounds.size.width/2, firstImage.bounds.size.height/2)];
    UITapGestureRecognizer *removeImageTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeImage_Tapped:)];
    removeImageTap.numberOfTapsRequired = 1;
    removeImageView.userInteractionEnabled = YES;
    [removeImageView addGestureRecognizer:removeImageTap];

    switch (i) {
        case 0:
            firstImage.image = [capturedImages objectAtIndex:i];
            //[firstImage addSubview:removeImageView];
            break;
        case 1:
            secondImage.image = [capturedImages objectAtIndex:i];
            [secondImage addSubview:removeImageView];
            break;
        case 2:
            thirdImage.image = [capturedImages objectAtIndex:i];
            [thirdImage addSubview:removeImageView];
            break;
        case 3:
            forthImage.image = [capturedImages objectAtIndex:i];
            [forthImage addSubview:removeImageView];
            break;
        default:
            break;
    }
}

请帮我解决。

1 个答案:

答案 0 :(得分:0)

设置属性sessionPreset,如下所示:

[yourcamera session].sessionPreset = AVCaptureSessionPresetHigh

这不会完全消除内存警告,但可能有助于减少它。