2016-08-20 11 views
7

ऑब्जेक्टिव सी में लिखा गया मेरा आईओएस कैमरा ऐप लॉक स्क्रीन से वापस आने पर/फोन को अनलॉक करते समय अपनी पूर्वावलोकन परत को फ्रीज करता है।एवीकैप्चर पूर्वावलोकन फोन को अनलॉक करते समय फ्रीज/अटक गया

सभी कैमरा कॉन्फ़िगरेशन सेटिंग्स viewWillAppear में कॉल की जा रही हैं। मैं अभी तक सफल रहा हूं, केवल एक ही समस्या को छोड़कर, जो कैमरा पूर्वावलोकन परत लॉक स्क्रीन से वापस आने पर फ्रीज या फंस गया है। मेरे कोड का कैमरा अनुभाग नीचे दिया गया है।

किसी भी मदद की बहुत सराहना की जाती है। धन्यवाद। ps: कृपया मेरे कोड में किसी भी गलती को इंगित करने के लिए स्वतंत्र महसूस करें क्योंकि मैं सिर्फ एक नौसिखिया हूं।

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
    dispatch_async(dispatch_get_main_queue(), ^{ 
    [self setGUIBasedOnMode]; 
    }); 
} 


-(void) setGUIBasedOnMode 
{ 
if (![self isStreamStarted]) { 
if (shutterActionMode == SnapCamSelectionModeLiveStream) 
{ 
    _flashButton.hidden = true; 
    _cameraButton.hidden = true; 
    _liveSteamSession = [[VCSimpleSession alloc] initWithVideoSize:[[UIScreen mainScreen]bounds].size frameRate:30 bitrate:1000000 useInterfaceOrientation:YES]; 
    [_liveSteamSession.previewView removeFromSuperview]; 
    AVCaptureVideoPreviewLayer *ptr; 
    [_liveSteamSession getCameraPreviewLayer:(&ptr)]; 
    _liveSteamSession.previewView.frame = self.view.bounds; 
    _liveSteamSession.delegate = self; 
} 
else{ 
    [_liveSteamSession.previewView removeFromSuperview]; 
    _liveSteamSession.delegate = nil; 
    _cameraButton.hidden = false; 
    if(flashFlag == 0){ 
     _flashButton.hidden = false; 
    } 
    else if(flashFlag == 1){ 
     _flashButton.hidden = true; 
    } 
    self.session = [[AVCaptureSession alloc] init]; 
    self.previewView.hidden = false; 
    self.previewView.session = self.session; 

    [self configureCameraSettings]; //All The Camera Configuration Settings. 

    dispatch_async(self.sessionQueue, ^{ 
     switch (self.setupResult) 
     { 
      case AVCamSetupResultSuccess: 
      { 
       [self addObservers]; 

       [self.session startRunning]; 

       self.sessionRunning = self.session.isRunning; 
       if(loadingCameraFlag == false){ 
        [self hidingView]; 
       } 
       break; 
      } 
      case AVCamSetupResultCameraNotAuthorized: 
      { 
       dispatch_async(dispatch_get_main_queue(), ^{ 
        NSString *message = NSLocalizedString(@"MyApp doesn't have permission to use the camera, please change privacy settings", @"Alert message when the user has denied access to the camera"); 
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"AVCam" message:message preferredStyle:UIAlertControllerStyleAlert]; 
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"Alert OK button") style:UIAlertActionStyleCancel handler:nil]; 
        [alertController addAction:cancelAction]; 

        UIAlertAction *settingsAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Settings", @"Alert button to open Settings") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
         [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]; 
        }]; 
        [alertController addAction:settingsAction]; 
        [self presentViewController:alertController animated:YES completion:nil]; 
       }); 
       break; 
      } 
      case AVCamSetupResultSessionConfigurationFailed: 
      { 
       dispatch_async(dispatch_get_main_queue(), ^{ 
        NSString *message = NSLocalizedString(@"Unable to capture media", @"Alert message when something goes wrong during capture session configuration"); 
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"MyApp" message:message preferredStyle:UIAlertControllerStyleAlert]; 
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"Alert OK button") style:UIAlertActionStyleCancel handler:nil]; 
        [alertController addAction:cancelAction]; 
        [self presentViewController:alertController animated:YES completion:nil]; 
       }); 
       break; 
      } 
     } 
    }); 
} 
} 

-(void)configureCameraSettings 
{ 
self.sessionQueue = dispatch_queue_create("session queue",  DISPATCH_QUEUE_SERIAL); 
self.setupResult = AVCamSetupResultSuccess; 
switch ([AVCaptureDevice  authorizationStatusForMediaType:AVMediaTypeVideo]) 
{ 
    case AVAuthorizationStatusAuthorized: 
    { 
     break; 
    } 
    case AVAuthorizationStatusNotDetermined: 
    { 
     dispatch_suspend(self.sessionQueue); 
     [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) { 
      if (! granted) { 
       self.setupResult = AVCamSetupResultCameraNotAuthorized; 
      } 
      dispatch_resume(self.sessionQueue); 
     }]; 
     break; 
    } 
    default: 
    { 
     self.setupResult = AVCamSetupResultCameraNotAuthorized; 
     break; 
    } 
} 

dispatch_async(self.sessionQueue, ^{ 
if (self.setupResult != AVCamSetupResultSuccess) { 
    return; 
} 
self.backgroundRecordingID = UIBackgroundTaskInvalid; 
NSError *error = nil; 

AVCaptureDevice *videoDevice = [IPhoneCameraViewController deviceWithMediaType:AVMediaTypeVideo preferringPosition:AVCaptureDevicePositionBack]; 
AVCaptureDeviceInput *videoDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error]; 

[self.session beginConfiguration]; 

if ([self.session canAddInput:videoDeviceInput]) { 
    [self.session addInput:videoDeviceInput]; 
    self.videoDeviceInput = videoDeviceInput; 

    dispatch_async(dispatch_get_main_queue(), ^{ 
     UIInterfaceOrientation statusBarOrientation = [UIApplication sharedApplication].statusBarOrientation; 
     AVCaptureVideoOrientation initialVideoOrientation = AVCaptureVideoOrientationPortrait; 
     if (statusBarOrientation != UIInterfaceOrientationUnknown) { 
      initialVideoOrientation = (AVCaptureVideoOrientation)statusBarOrientation; 
     } 
     AVCaptureVideoPreviewLayer *previewLayer = (AVCaptureVideoPreviewLayer *)self.previewView.layer; 
     if (shutterActionMode == SnapCamSelectionModeVideo) 
     { 
      [previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill]; 
      if([self.session canSetSessionPreset:AVCaptureSessionPresetMedium]){ 
       [self.session setSessionPreset:AVCaptureSessionPresetMedium]; 
      } 
     } 
     previewLayer.connection.videoOrientation = initialVideoOrientation; 
    }); 
} 
else { 
    self.setupResult = AVCamSetupResultSessionConfigurationFailed; 
} 

AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio]; 
AVCaptureDeviceInput *audioDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:&error]; 

if (! audioDeviceInput) { 
} 

if ([self.session canAddInput:audioDeviceInput]) { 
    [self.session addInput:audioDeviceInput]; 
} 
else { 
} 

AVCaptureMovieFileOutput *movieFileOutput = [[AVCaptureMovieFileOutput alloc] init]; 

Float64 TotalSeconds = 10*60; 
int32_t preferredTimeScale = 30; 
CMTime maxDuration = CMTimeMakeWithSeconds(TotalSeconds, preferredTimeScale);   movieFileOutput.maxRecordedDuration = maxDuration; 
movieFileOutput.minFreeDiskSpaceLimit = 1024 * 1024 * 100; 

if ([self.session canAddOutput:movieFileOutput]) { 
    [self.session addOutput:movieFileOutput]; 
    AVCaptureConnection *connection = [movieFileOutput connectionWithMediaType:AVMediaTypeVideo]; 
    if (connection.isVideoStabilizationSupported) { 
     connection.preferredVideoStabilizationMode = AVCaptureVideoStabilizationModeAuto; 
    } 
    self.movieFileOutput = movieFileOutput; 
} 
else { 
    self.setupResult = AVCamSetupResultSessionConfigurationFailed; 
} 

AVCaptureStillImageOutput *stillImageOutput = [[AVCaptureStillImageOutput alloc] init]; 
if ([self.session canAddOutput:stillImageOutput]) { 
    stillImageOutput.outputSettings = @{AVVideoCodecKey : AVVideoCodecJPEG}; 
    [self.session addOutput:stillImageOutput]; 
    self.stillImageOutput = stillImageOutput; 
} 
else { 
    self.setupResult = AVCamSetupResultSessionConfigurationFailed; 
} 
[self.session commitConfiguration]; 
}); 
} 
+0

http://stackoverflow.com/a/30707170/4601170 –

उत्तर

0

ViewDidLoadViewWillAppear और ViewDidAppear विधि एप्लिकेशन जीवन चक्र के निष्पादन के बीच एक बहुत अंतर नहीं है। UIViews की

निर्माण या कुछ भारी काम को क्रियान्वित काफी महंगा कारणों फ्रीज है, और आप ViewWillAppear विधि

पर कर रही है एक नज़र जितना संभव हो उतना से बचना चाहिए:

  1. ViewDidLoad: जब भी मैं एक दृश्य में नियंत्रण जोड़ रहा हूं जो दृश्य के साथ एक साथ दिखना चाहिए, तुरंत, मैंने इसे ViewDidLoad विधि में रखा है। मूल रूप से जब भी दृश्य स्मृति में लोड किया गया था, तो इस विधि को बुलाया जाता है। तो उदाहरण के लिए, यदि मेरा विचार 3 लेबल वाला एक रूप है, तो मैं यहां लेबल जोड़ूंगा; दृश्य उन रूपों के बिना कभी अस्तित्व में नहीं रहेगा।
  2. व्यूविल्लएपियर: व्यूविल्लएपियर आम तौर पर फॉर्म पर डेटा अपडेट करने के लिए। तो, ऊपर दिए गए उदाहरण के लिए, मैं वास्तव में अपने डोमेन से डेटा को फ़ॉर्म में लोड करने के लिए इसका उपयोग करूंगा। UIViews का निर्माण काफी महंगा है, और आपको व्यूविल्लएपियर विधि पर जितना संभव हो उतना टालना चाहिए, क्योंकि जब इसे कॉल किया जाता है, तो इसका मतलब है कि आईफोन उपयोगकर्ता को UIView दिखाने के लिए पहले से तैयार है, और आप यहां कुछ भी भारी करते हैं प्रदर्शन को बहुत ही स्पष्ट तरीके से प्रभावित करेगा (जैसे एनिमेशन में देरी हो रही है, आदि)।
  3. ViewDidAppear: ViewDidAppear को उन चीज़ों के लिए नए थ्रेड शुरू करने में लंबा समय लगेगा, उदाहरण के लिए ऊपर दिए गए फॉर्म के लिए अतिरिक्त डेटा प्राप्त करने के लिए एक webservice कॉल करना। अच्छी बात यह है कि दृश्य पहले से मौजूद है और उपयोगकर्ता को प्रदर्शित किया जा रहा है, आप डेटा प्राप्त करते समय उपयोगकर्ता को एक अच्छा "प्रतीक्षा" संदेश दिखा सकते हैं। को रोकने के लिए UIApplicationDidEnterBackgroundNotification/UIApplicationWillEnterForegroundNotification,UIApplicationWillResignActiveNotification/UIApplicationDidBecomeActiveNotification अधिसूचना का पालन करने के
1

ट्राई/अपने कब्जा सत्र शुरू तदनुसार

संबंधित मुद्दे

 संबंधित मुद्दे