我有UIImagePickerController
可用于在我的社交网络应用程序中上传个人资料图片。
单独使用时工作正常,即没有其他相机干扰。
在另一个视图中,我使用AVCaptureSession
和AVCaptureVideoPreviewLayer
将摄像机视图嵌入到视图中。在这里,用户可以上传他们捕获的各种照片。
单独使用时也可正常工作,即没有其他相机干扰。
(这是一个标签栏应用程序)
每当AVCapturePreviewLayer
处于活动状态,并且我使用UIImagePickerController
进入视图时,imagePicker需要很长时间才能加载,有时它会冻结。
这是我初始化AVSession/AVCapturePreviewLayer
:
self.session = [[AVCaptureSession alloc] init];
self.session.sessionPreset = AVCaptureSessionPreset640x480;
self.captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
self.captureVideoPreviewLayer.frame = self.cameraView.bounds;
[self.captureVideoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[self.cameraView.layer addSublayer:self.captureVideoPreviewLayer];
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if (!input) {
// Handle the error appropriately.
NSLog(@"ERROR: trying to open camera: %@", error);
}else
[self.session addInput:input];
self.stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
[self.stillImageOutput setOutputSettings:outputSettings];
[self.session addOutput:self.stillImageOutput];
这是我初始化UIImagePickerController
:
self.picker = [[UIImagePickerController alloc] init];
self.picker.delegate = self;
self.picker.allowsEditing = YES;
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:self.picker animated:YES completion:NULL];
UIImagePickerController
会永远加载?UIImagePickerController
?答案 0 :(得分:3)
确定。这似乎在呼唤:
[AVSession stopRunning];
viewDidDisappear:(BOOL)animated
中的
为我解决了这个问题。