我需要使用AVCaptureSession显示相机输出。我的代码适用于除iPhone 3Gs以外的所有设备:
- (void)viewDidLoad
{
[super viewDidLoad];
// Receiving events
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didGetError:) name:AVCaptureSessionRuntimeErrorNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didStartRunning:) name:AVCaptureSessionDidStartRunningNotification object:nil];
self.session = [[AVCaptureSession alloc] init];
NSString *preset = AVCaptureSessionPresetLow;
self.session.sessionPreset = preset;
self.device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
self.input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
[self.session addInput:self.input];
// Setting up the preview layer. It causes the error.
self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
[self.view.layer addSublayer:self.previewLayer];
self.previewLayer.frame = self.view.bounds;
[self.session startRunning];
}
- (void)didStartRunning:(NSNotification *)notification
{
NSLog(@"started");
}
- (void)didGetError:(NSNotification *)notification
{
NSError *error = notification.userInfo[AVCaptureSessionErrorKey];
NSLog(@"error: %@", error.localizedDescription);
}
输出结果为:
错误:操作无法完成
开始
你能告诉我解决方法吗? Apple的AVCam示例也不适用于3G。