我有一个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
的加载时间
好的。似乎在呼叫:
[AVSession stopRunning];
在viewDidDisappear:(BOOL)animated
中
为我修复了这个问题。