我使用以下代码从IPAD前置摄像头自动拍摄照片:
UIImagePickerController *imgPkr = [[UIImagePickerController alloc] init];
imgPkr.sourceType = UIImagePickerControllerSourceTypeCamera;
imgPkr.delegate = self;
imgPkr.cameraDevice=UIImagePickerControllerCameraDeviceFront;
[self presentModalViewController:imgPkr animated:YES];
imgPkr.showsCameraControls = NO;
[imgPkr takePicture];
但是这段代码不要拍任何照片,也不要给代表打电话:
-(void)imagePickerController:(UIImagePickerController*)picker
didFinishPickingMediaWithInfo:(NSDictionary*)info
知道代码有什么问题
答案 0 :(得分:4)
我的第一个猜测是在呈现选择器之前调用了[imgPkr takePicture];
。试试这样:
UIImagePickerController *imgPkr = [[UIImagePickerController alloc] init];
imgPkr.sourceType = UIImagePickerControllerSourceTypeCamera;
imgPkr.delegate = self;
imgPkr.cameraDevice=UIImagePickerControllerCameraDeviceFront;
[self presentModalViewController:imgPkr animated:YES];
imgPkr.showsCameraControls = NO;
[self performSelector:@selector(takePicture) withObject:self afterDelay:1.0];
OR
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(takePicture)
name:AVCaptureSessionDidStartRunningNotification
object:nil];
&安培;
- (void)takePicture
{
[imgPkr takePicture];
}
答案 1 :(得分:2)
我打赌你在日志中收到一条消息说
UIImagePickerController:忽略拍照请求;相机是 尚未准备好。
这是一个常见问题,因为底层捕获会话需要一些时间才能启动,因此您必须确保相机已准备好拍照并然后调用takePicture
方法。现在,我在这里的答案中详细解释了获得通知的方法:How to know if iPhone camera is ready to take picture?
请注意,此方法适用于iOS5 +(旧版本中存在一个错误,可防止此事件的系统通知,与文档中描述的相反)。我希望这会有所帮助。
答案 2 :(得分:0)
等待相机准备好的另一种方法,直到拍照完成块 - >
[self presentViewController:imagePicker animated:YES
completion:^ {
[imagePicker takePicture];
}];
谢谢你'GrandSteph'来
iOS taking photo programmatically