2012-06-04 7 views
5

시간의 90 %에서 작동하는 카메라 미리보기 창이 있습니다. 그러나 배경에있는 경우 내 앱으로 돌아갈 때 미리보기가 표시되지 않는 경우가 있습니다. 이 뷰를로드 할 때 내가 전화 코드입니다 :배경에서 돌아올 때 AVCaptureSession이 실패합니다.

- (void) startCamera { 

session = [[AVCaptureSession alloc] init]; 
session.sessionPreset = AVCaptureSessionPresetPhoto; 

AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session]; 
captureVideoPreviewLayer.frame = _cameraView.bounds; 
[_cameraView.layer addSublayer:captureVideoPreviewLayer]; 
captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 
captureVideoPreviewLayer.position=CGPointMake(CGRectGetMidX(_cameraView.bounds), 160); 

AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 
NSError *error = nil; 

AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error]; 
if (!input) { 

    NSLog(@"ERROR: %@", error); 


    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Important!" 
                message:@"Unable to find a camera." 
                delegate:nil 
              cancelButtonTitle:@"Ok" 
              otherButtonTitles:nil]; 
    [alert show]; 
    [alert autorelease]; 
} 

[session addInput:input]; 

stillImage = [[AVCaptureStillImageOutput alloc] init]; 
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG , AVVideoCodecKey, nil]; 
[stillImage setOutputSettings:outputSettings]; 

[session addOutput:stillImage]; 
[session startRunning]; 
} 

이 경우, 내 기본보기로 전환 할 수 있으며 다시과 알이 잘이지만, 내가 죽이고 싶은 성가신 버그. 미리보기 창은 내 스토리 보드의 UIView입니다.

답변

7

뷰로드시 캡처 세션을 시작하지 말고 viewWillAppear에서 시작하고 viewWillDissapear에서 중지하십시오.

앱이 배경에있을 때보기 컨트롤러가 일부 메모리를 정리하고있는 것 같습니다. 이를 염두에두고 캡처 세션을 초기화해야합니다.

시작 메소드 대신 개인 속성 getter 메소드에 느리게 세션을 할당하면 메모리 누출을 방지 할 수 있습니다.

+1

감사합니다. 몇 시간 동안 테스트 해 보겠습니다. – mrEmpty

+0

환상적인 해결책 ....... –

+2

기다림 -'viewWillAppear' /'Disappear' 만 호출되는 경우가 아닙니까? 장면이 앱에서 움직이는 것처럼 : 앱이 전경을오고 갈 때 호출되지 않습니다. 'UIApplicationWillResignActiveNotification'에 등록 할 필요가 없을까요? [그 예와 품질 보증] (http://stackoverflow.com/a/22868754/294884) – Fattie

관련 문제