在iPhone上拍照而不显示控件

Rob*_*ner 22 camera ios

有没有办法在不通过Apple控件的情况下在iPhone上的代码中拍照?我见过很多应用程序,但我不确定使用什么API调用.

Khu*_*Ali 37

编辑:正如下面的评论中所建议的,我现在已经明确地展示了如何声明和初始化AVCaptureSession.似乎有少数人正在初始化错误或将AVCaptureSession声明为方法中的局部变量.这不行.

以下代码允许使用AVCaptureSession拍摄照片而无需用户输入:

// Get all cameras in the application and find the frontal camera.
AVCaptureDevice *frontalCamera;
NSArray *allCameras = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];

// Find the frontal camera.
for ( int i = 0; i < allCameras.count; i++ ) {
    AVCaptureDevice *camera = [allCameras objectAtIndex:i];

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

// If we did not find the camera then do not take picture.
if ( frontalCamera != nil ) {
    // Start the process of getting a picture.
    session = [[AVCaptureSession alloc] init];

    // Setup instance of input with frontal camera and add to session.
    NSError *error;
    AVCaptureDeviceInput *input =
    [AVCaptureDeviceInput deviceInputWithDevice:frontalCamera error:&error];

    if ( !error && [session canAddInput:input] ) {
        // Add frontal camera to this session.
        [session addInput:input];

        // We need to capture still image.
        AVCaptureStillImageOutput *output = [[AVCaptureStillImageOutput alloc] init];

        // Captured image. settings.
        [output setOutputSettings:
        [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey,nil]];

        if ( [session canAddOutput:output] ) {
            [session 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; }
            }

            // Finally take the picture
            if ( videoConnection ) {
                [session startRunning];

                [output captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {

                    if (imageDataSampleBuffer != NULL) {
                        NSData *imageData = [AVCaptureStillImageOutput 
                                  jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
                        UIImage *photo = [[UIImage alloc] initWithData:imageData];                                                                 
                    }

                }];
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

session变量的类型为AVCaptureSession,并已在类的.h文件中声明(作为属性或类的私有成员):

AVCaptureSession *session;
Run Code Online (Sandbox Code Playgroud)

然后需要在某个地方初始化它,例如在类的init方法中:

session = [[AVCaptureSession alloc] init]
Run Code Online (Sandbox Code Playgroud)

  • 现在尝试这个代码,它一直工作直到`[输出captureStillImageAsynchronouslyFromConnection:...`但然后停止,导致照片从未实际被拍摄.我在此之后立即将记录添加到该行,它永远不会被记录.代码是逐字写的,我已经确认了代码,直到那一点工作(添加了一个按钮并将其连接到.xib中,正确触发).我还包括AVFoundation.framework和AssetsLibrary.framework.有任何想法吗? (4认同)

Dre*_*w C 25

是的,有两种方法可以做到这一点.iOS 3.0+中提供的一种方法是使用UIImagePickerController该类,将该showsCameraControls属性设置为NO,并将该cameraOverlayView属性设置为您自己的自定义控件.两个,在iOS 4.0+中可用,配置一个AVCaptureSession,AVCaptureDeviceInput使用适当的相机设备,和AVCaptureStillImageOutput.第一种方法更简单,适用于更多iOS版本,但第二种方法可以更好地控制照片分辨率和文件选项.