简体   繁体   中英

(display a picture in a UIImageView) crash on captureStillImageAsynchronouslyFromConnection

I try to display a picture of the camera in a UIImageView.

I try to debug my application (whit @property (nonatomic,weak) IBOutlet UILabel *messageLabel; ),

-------Line "[output captureStillImageAsynchronouslyFromConnection ..."

I have the value assigned before the call. (self.messageLabel.text = @"before !!"; I never have the value @"Test !!")

the return of the variable result is "OK"

I use Entitlements: com.apple.security.device.camera

Can you help me debug more detail.

Here is my code

-(NSString*)  takePhoto
{
    AVCaptureDevice *frontalCamera;
    AVCaptureSession *photoSession;
    NSString* result;

    NSArray *allCameras = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    for ( int i = 0; i < allCameras.count; i++ )
    {
        AVCaptureDevice *camera = [allCameras objectAtIndex:i];

        if ( camera.position == AVCaptureDevicePositionFront )
        {
            frontalCamera = camera;
        }
    }

    if ( frontalCamera != nil )
    {
        photoSession = [[AVCaptureSession alloc] init];

        NSError *error;
        AVCaptureDeviceInput *input =
        [AVCaptureDeviceInput deviceInputWithDevice:frontalCamera error:&error];
        if ( !error && [photoSession canAddInput:input] )
        {   
            [photoSession addInput:input];

            AVCaptureStillImageOutput *output = [[AVCaptureStillImageOutput alloc] init];

            [output setOutputSettings:
             [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey,nil]];
            if ( [photoSession canAddOutput:output] )
            {
                [photoSession addOutput:output];

                AVCaptureConnection *videoConnection = nil;

                for (AVCaptureConnection *connection in output.connections)
                {
                    for (AVCaptureInputPort *port in [connection inputPorts])
                    {
                        if ([[port mediaType] isEqual:AVMediaTypeVideo] )
                        {
                            videoConnection = connection;
                            break;
                        }
                    }
                    if (videoConnection) { break; }
                }
                if ( videoConnection )
                {

                    [photoSession startRunning];
                    result = @"Let's Go ?";
                    self.messageLabel.text = @"before !!";
                    [output captureStillImageAsynchronouslyFromConnection:videoConnection
                                                        completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
                                                            self.messageLabel.text = @"Test !!";
                                                            if (imageDataSampleBuffer != NULL)
                                                            {
                                                                NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
                                                                UIImage *photo = [[UIImage alloc] initWithData:imageData];
                                                                self.vImage.image = photo;

                                                            }
                                                        }];
                    result = @"OK";
                }
            }
        }
    }
    return result;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM