(在UIImageView中显示图片)在captureStillImageAsynchronouslyFromConnection上崩溃

时间:2015-01-14 20:18:37

标签: ios xcode debugging camera avcapturedevice

我尝试在UIImageView中显示相机的图片。

我尝试调试我的应用程序(whit @property(非原子,弱)IBOutlet UILabel * messageLabel; ),

------- Line“[输出captureStillImageAsynchronouslyFromConnection ...”

我在通话前分配了值。 (self.messageLabel.text = @“before !!”;我从来没有价值@“Test !!”)

变量结果的返回是“OK”

我使用权利: com.apple.security.device.camera

你能帮我调试更多细节。

这是我的代码

-(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;
}

0 个答案:

没有答案