我需要使用 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。