0

你好,我的应用程序在我使用拍照时崩溃,AVCaptureSession当我在 2-3 秒后弹出到我的相机视图应用程序崩溃时转到另一个视图。
我将此代码用于使用AVCaptureSession.

session = [[AVCaptureSession alloc] init];
    session.sessionPreset = AVCaptureSessionPresetPhoto;

     captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
    [captureVideoPreviewLayer setBackgroundColor:[[UIColor blackColor] CGColor]];
    [captureVideoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
    rootLayer = [_MainPreviewVIEW layer];

    [rootLayer addSublayer:captureVideoPreviewLayer];

    NSArray *devices = [AVCaptureDevice devices];

    for (AVCaptureDevice *device in devices)
    {
        NSLog(@"Device name: %@", [device localizedName]);

        if ([device hasMediaType:AVMediaTypeVideo])
        {
            if ([device position] == AVCaptureDevicePositionBack)
            {
                NSLog(@"Device position : back");
                backCamera = device;
                [device lockForConfiguration:nil];
                [device unlockForConfiguration];
            }
        }
    }

    NSError *error = nil;
    input = [AVCaptureDeviceInput deviceInputWithDevice:backCamera error:&error];
    if (!input)
    {
        NSLog(@"ERROR: trying to open camera: %@", error);
    }
    [session addInput:input];
    stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
    NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
    [stillImageOutput setOutputSettings:outputSettings];

    [session addOutput:stillImageOutput];

    [session startRunning];

我收到此错误消息。

[Viewcontroller respondsToSelector:]:消息发送到已释放实例 0x148286690**

我用这种方法拍照。

AVCaptureConnection *videoConnection = nil;
    for (AVCaptureConnection *connection1 in stillImageOutput.connections)
    {
        for (AVCaptureInputPort *port in [connection1 inputPorts])
        {
            if ([[port mediaType] isEqual:AVMediaTypeVideo] )
            {
                videoConnection = connection1;
                break;
            }
        }
        if (videoConnection)
        {
            break;
        }
    }
    NSLog(@"about to request a capture from: %@", stillImageOutput);
    [stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
    {
        if (imageSampleBuffer != NULL)
        {
            NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
            Catchimg=[[UIImage alloc]initWithData:imageData];

            [session stopRunning];
        }
    }];

然后在我使用导航控制器使用简单的推送视图之后。
当我再次回到这个相机视图时,它会在 2-3 秒后崩溃。

4

0 回答 0