だから私は自分でそれを考え出しました。このコードは、私の作品と少なくともUI凍結生成:
- (void)willMoveToSuperview:(UIView *)newSuperview {
//capture session setup
AVCaptureDeviceInput *newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:self.rearCamera error:nil];
AVCaptureStillImageOutput *newStillImageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:
AVVideoCodecJPEG, AVVideoCodecKey,
nil];
[newStillImageOutput setOutputSettings:outputSettings];
AVCaptureSession *newCaptureSession = [[AVCaptureSession alloc] init];
if ([newCaptureSession canAddInput:newVideoInput]) {
[newCaptureSession addInput:newVideoInput];
}
if ([newCaptureSession canAddOutput:newStillImageOutput]) {
[newCaptureSession addOutput:newStillImageOutput];
self.stillImageOutput = newStillImageOutput;
self.captureSession = newCaptureSession;
}
// -startRunning will only return when the session started (-> the camera is then ready)
dispatch_queue_t layerQ = dispatch_queue_create("layerQ", NULL);
dispatch_async(layerQ, ^{
[self.captureSession startRunning];
AVCaptureVideoPreviewLayer *prevLayer = [[AVCaptureVideoPreviewLayer alloc]initWithSession:self.captureSession];
prevLayer.frame = self.previewLayerFrame;
prevLayer.masksToBounds = YES;
prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
prevLayer.cornerRadius = PREVIEW_LAYER_EDGE_RADIUS;
//to make sure were not modifying the UI on a thread other than the main thread, use dispatch_async w/ dispatch_get_main_queue
dispatch_async(dispatch_get_main_queue(), ^{
[self.layer insertSublayer:prevLayer atIndex:0];
});
});
}
ありがとうございます!私のための鍵はバックグラウンドスレッドで 'AVCaptureSession'の' startRunning'と 'stopRunning'を実行していました。 – the4kman