出现UIImagePIckerController但相机无法启动

时间:2013-01-01 19:47:28

标签: objective-c uiviewcontroller uiimagepickercontroller xcode4.5

是的,这可能是this的重复问题。但由于它没有得到答案,我将更具体地说明案例和代码:

我有3个参与UIViewControllers

  1. WelcomeView - 第一个
  2. TakePhotoViewController - 第二个是OverlayviewController的代表
  3. OverlayViewController - 自定义相机视图。
  4. 情景:

    1. 用户输入WelcomeView并点击要转移到TakeView的按钮。
    2. UIImageViewController正在打开拍照。
    3. 用户点击取消按钮 - 正在调用didCancelCamera中的TakePhotoViewController方法并返回WelcomeView
    4. 用户离开应用。
    5. 用户重新打开应用并再次执行第1步。
    6. 图像采摘器未打开。我可以拍摄一张照片并且它没问题 - 但是用户无法看到他正在拍摄的东西。

      OverlayViewController.h
      
      @interface OverlayViewController : BaseViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
      @property (nonatomic,weak) id<OverlayViewControllerDelegate> delegate;
      @property (nonatomic,retain) UIImagePickerController *imagePickerController;
      @end
      
      OverlayViewController.m:
      
      
      - (id)initWithCoder:(NSCoder *)aDecoder
      {
          self = [super initWithCoder:aDecoder];
          if (self) {
              self.imagePickerController = [[UIImagePickerController alloc] init];
              self.imagePickerController.delegate = self;
          }
          return self;
      }
      
      
      - (void)viewDidLoad
      {   
          [super viewDidLoad];
          self.view.backgroundColor = [UIColor clearColor];
      }
      
      - (IBAction)takePicture:(id)sender {
          [self.imagePickerController takePicture];
      }
      
      - (IBAction)cancelImagePicker:(id)sender {
          [self.delegate didCancelCamera];
      }
      
      - (void) setupImagePicker:(UIImagePickerControllerSourceType) sourceType
      {
          self.imagePickerController.sourceType = sourceType;
          if (sourceType == UIImagePickerControllerSourceTypeCamera)
          {
              self.imagePickerController.showsCameraControls = NO;
              if ([[self.imagePickerController.cameraOverlayView subviews] count] ==0)
              {
                  CGRect overlayViewFrame = self.imagePickerController.cameraOverlayView.frame;
                  CGRect newFrame = CGRectMake(0.0, CGRectGetHeight(overlayViewFrame)-self.view.frame.size.height-10.0, CGRectGetWidth(overlayViewFrame), self.view.frame.size.height + 10.0);
                  self.view.frame = newFrame;
                  [self.imagePickerController.cameraOverlayView addSubview:self.view];
              }
          }
      }
      
      
      - (void)finishAndUpdate
      {
          [NSThread detachNewThreadSelector:@selector(threadStartAnimating:) toTarget:self withObject:nil];
          [self.delegate didFinishWithCamera];  // tell our delegate we are done with the camera
      }
      
      - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
      {
          [self finishAndUpdate];
      }
      
      TakePhotoViewController.h
      
      @interface TakePhotoViewController : BaseViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate,OverlayViewControllerDelegate>
      @property (nonatomic, retain) OverlayViewController *overlayViewController;
      @end
      
      TakePhotoViewController.m
      
      - (void)viewDidLoad
      {
          [super viewDidLoad];
          UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
          // Insert the overlay
          self.overlayViewController = (OverlayViewController *)[sb instantiateViewControllerWithIdentifier:@"Overlay"];
          self.overlayViewController.delegate = self;   
      }
      - (void)viewDidUnload
      {
          self.overlayViewController = nil;
          [super viewDidUnload];
          // Release any retained subviews of the main view.
      }
      - (void)openImagePicker {
         if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
             [self showImagePicker:UIImagePickerControllerSourceTypeCamera];
         }
         else{
             [self showImagePicker:UIImagePickerControllerSourceTypePhotoLibrary];
         }
      }
      
      - (void)viewDidAppear:(BOOL)animated{
          if (appDelegate.shouldOpenPicker){
            [self openImagePicker];
          }
      }
      - (void)showImagePicker:(UIImagePickerControllerSourceType)sourceType
      {
      
          if ([UIImagePickerController isSourceTypeAvailable:sourceType])
          {
             [self.overlayViewController setupImagePicker:sourceType];
             [self presentViewController:self.overlayViewController.imagePickerController animated:YES completion:nil];
          }
      }
      -(void)didCancelCamera{
          [[self.overlayViewController.imagePickerController presentingViewController] dismissViewControllerAnimated:NO completion:^ {
              [self performSegueWithIdentifier:@"fromTakeToWelcome" sender:self];
          }];
      
      }
      

1 个答案:

答案 0 :(得分:0)

我发现了这个错误。

方法

当用户点击-(void)didCancelCamera中的TakePhotoViewController时,系统会调用来自- (IBAction)cancelImagePicker:(id)sender的{​​p> OverlayViewController

但是,didCancelCamera中的代码会以某种方式导致viewDidAppear的{​​{1}}方法再次被调用并重新打开图像选择器。

我不知道为什么

TakePhotoViewController

导致再次调用该视图的viewDidAppear方法(TakePhoto)。

希望它会帮助某人