2

每次我启动 AVCaptureSession 时,我都会收到一条内存警告,导致一段时间后崩溃。
我正在异步启动会话,仪器工具说应用程序消耗大约 2 MB 内存。你知道如何克服这个问题吗?2MB 分配的内存太多了吗?

谢谢![iOS 4.3,ARC]

@autoreleasepool {
    //Init capture session
    session = [[AVCaptureSession alloc] init];
    session.sessionPreset = AVCaptureSessionPresetPhoto;


    //Resize container view
    CGRect cameraContainerFrame = cameraContainerView.frame;
    cameraContainerFrame.size = CGSizeMake(320, 426);
    cameraContainerView.frame = cameraContainerFrame;

    CALayer *viewLayer = [cameraContainerView layer];
    [viewLayer setMasksToBounds:YES];

    //Create preview layer
    captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];

    CGRect bounds = [cameraContainerView bounds];
    [captureVideoPreviewLayer setFrame:bounds];


    if ([captureVideoPreviewLayer isOrientationSupported]) {
        [captureVideoPreviewLayer setOrientation:AVCaptureVideoOrientationPortrait];
    }    
    [captureVideoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];

    [viewLayer addSublayer:captureVideoPreviewLayer];

    //Get input device
    captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    if ([captureDevice lockForConfiguration:nil]){
        captureDevice.focusMode = AVCaptureFocusModeContinuousAutoFocus;
        captureDevice.whiteBalanceMode = AVCaptureWhiteBalanceModeContinuousAutoWhiteBalance;
        [captureDevice unlockForConfiguration];
    }

    NSError *error = nil;
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
    if (!input) {
        // Handle the error appropriately.
        DLog(@"ERROR: trying to open camera: %@", error);
    }
    //Add input to session
    [session addInput:input];

    //Output
    stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
    NSDictionary *outputSettings = [NSDictionary dictionaryWithObject:AVVideoCodecJPEG forKey:AVVideoCodecKey];
    [stillImageOutput setOutputSettings:outputSettings];
    [session addOutput:stillImageOutput];

    //Save state
    cameraSessionInitialized = YES;
    [session startRunning];
}
4

1 回答 1

9
session.sessionPreset = AVCaptureSessionPresetMedium;

If you don't care about quality, this does get rid of memory warnings. I'm trying to figure out how to get it working with the AVCaptureSessionPresetPhoto.

于 2013-04-26T23:21:45.650 回答