处理captureStillImageAsynchronouslyFromConnection:completionHandler:

时间:2015-02-06 04:45:31

标签: ios xcode

我一直在尝试设置一个小型iOS方法,当用户打开应用程序时会自动拍照。经过大量的研究,我终于找到了这个iOS taking photo programmatically,经过一段时间后我发现这个来自苹果https://developer.apple.com/library/ios/samplecode/AVCam/Introduction/Intro.html,有人可以帮助我开始设置captureStillImageAsynchronouslyFromConnection:completionHandler:的方法 我不希望所述用户进行任何互动。感谢

1 个答案:

答案 0 :(得分:1)

即使你似乎想要异步进行调用并使用AVFoundation,我还是建议在这种情况下只使用UIImagePickerController,例如:

- (void)viewDidLoad {

    [super viewDidLoad];

    // If the device has a camera...
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

        // Create an image picker
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront;
        imagePickerController.showsCameraControls = NO;
        imagePickerController.delegate = self;

        [self presentViewController:imagePickerController animated:YES completion:^{

            // And take the picture after a short delay
            // to give the view and image picker time to get
            // ready
            [self performSelector:@selector(takepic:) withObject:imagePickerController afterDelay:2];
        }];
    }
}

// Automatically take the picture using the 
// image picker passed in as a parameter
- (void)takepic:(UIImagePickerController*)imagePickerController {
    [imagePickerController takePicture];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

    // ... Do whatever with the image ...

    [picker dismissViewControllerAnimated:YES completion:nil];
}