【问题标题】:Camera not presenting in ipad running iOS 8.2运行 iOS 8.2 的 ipad 中未显示相机
【发布时间】:2015-09-24 06:29:07
【问题描述】:

我正在开发通用应用程序并想打开相机。现在我可以在 iphone 上使用 UIimagePickerController 打开相机,但在 iPad 上它不起作用。我已经搜索了解决方案并找到了此代码

self.cameraController = [[UIImagePickerController alloc] init];
self.cameraController.sourceType = UIImagePickerControllerSourceTypeCamera;


    self.cameraController.modalPresentationStyle = UIModalPresentationCurrentContext;

self.cameraController.showsCameraControls = NO;
self.cameraController.navigationBarHidden = YES;
self.cameraController.wantsFullScreenLayout = NO;

[self.cameraController setCameraOverlayView:ar_overlayView];
[ar_overlayView setFrame:self.cameraController.view.bounds];

UIViewController *vc = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
if([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0 && UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{

        [self presentViewController:self.cameraController animated:YES completion:nil];
    }];

}
else{
    [vc presentViewController:self.cameraController animated:YES completion:nil];
}

还遵循了一些教程,例如

TechnoTopia Post 但没有找到任何运气。我已经在 iphone 5s 上对其进行了测试,并且在其中运行良好,但在 iPad mini 上,相机没有显示。任何帮助将不胜感激!

【问题讨论】:

  • @gansai 不,它不是重复的,请在标记为重复之前阅读这两个问题。你提到的问题是关于前置摄像头的,这个问题是关于开放式摄像头的。
  • 它不重复任何问题,因为我在打开相机而不是前置相机时遇到问题。

标签: ios iphone ipad uiimagepickercontroller


【解决方案1】:

在 iPad 上试试这个

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex==0)
{
     [[NSOperationQueue mainQueue] addOperationWithBlock:^{
 UIImagePickerController* picker=[[UIImagePickerController alloc]init];
    picker.delegate=self;
    picker.allowsEditing=YES;
    picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:picker animated:YES completion:nil];
     }];
}
else if (buttonIndex==1)
{
     [[NSOperationQueue mainQueue] addOperationWithBlock:^{
 UIImagePickerController* picker=[[UIImagePickerController alloc]init];
    picker.delegate=self;
    picker.allowsEditing=YES;
    picker.sourceType=UIImagePickerControllerSourceTypeCamera;
         [self presentViewController:picker animated:YES completion:nil];}];
}
}

【讨论】:

    【解决方案2】:

    对于 iPad,您应该这样做:

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
    UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:picker];
    [popover presentPopoverFromRect:self.selectedImageView.bounds inView:self.selectedImageView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    self.popOver = popover;
    }else {
    [self presentModalViewController:picker animated:YES];
    }
    

    .h 文件中声明的位置:

    @property (nonatomic, strong) UIPopoverController *popOver;
    

    您应该处理弹出框委托:

    -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
    
    -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 
    

    苹果文档: apple link

    它声明:

    "通过调用当前活动视图控制器的 presentViewController:animated:completion: 方法呈现用户界面,将您配置的图像选择器控制器作为新的视图控制器传递。在 iPad 上,使用弹出框呈现用户界面。这样做仅当图像选择器控制器的 sourceType 属性设置为 UIImagePickerControllerSourceTypeCamera 时才有效。"

    【讨论】:

      【解决方案3】:

      试试这个代码:

      UIImagePickerController *aImgPickerController = [[UIImagePickerController alloc] init];
      aImgPickerController.delegate = self;
      aImgPickerController.allowsEditing = YES;
      
      aImgPickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
      
      dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
          [self presentViewController:aImgPickerController animated:NO completion:^{
              NSLog(@"success present picker");
          }];
      });
      

      【讨论】:

      • 嘿,我把 presentViewController:animated:NO completion: 放在调度块中有什么用,因为它在 iphone 中工作正常......你能解释一下吗?
      【解决方案4】:
      - (IBAction)onClickSelectPicture:(id)sender 
      {
          if( [UIImagePickerController isCameraDeviceAvailable: UIImagePickerControllerCameraDeviceFront ])
          {
              UIImagePickerController *picker = [[UIImagePickerController alloc] init];
              picker.delegate = self;
              picker.allowsEditing = YES;
              picker.sourceType=UIImagePickerControllerSourceTypeCamera;
              picker.cameraDevice=UIImagePickerControllerCameraDeviceFront;
              [self presentViewController:picker animated:YES completion:NULL];
          }
          else
          {
              UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                                    message:@"Device has no camera"
                                                                   delegate:nil
                                                          cancelButtonTitle:@"OK"
                                                          otherButtonTitles: nil];
      
              [myAlertView show];
          }
      }
      
      - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
      {
      
          imgProf = info[UIImagePickerControllerEditedImage];
              [viewNewPhoto setImage:imgProf];
      
          imgProf = [self imageWithImage:imgProf scaledToSize:CGSizeMake(imgProf.size.width/4, imgProf.size.height/4)];
      
          if (imgProf == nil)
          {
              imgPhotoString = @"NoPhoto";
          }else
          {
              imgPhotoString = [self getStringFromImage:imgProf];
          }
      
          [picker dismissViewControllerAnimated:YES completion:NULL];
      }
      
      - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
      {
          [picker dismissViewControllerAnimated:YES completion:NULL];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-17
        • 2020-11-09
        • 2021-06-01
        • 1970-01-01
        • 2023-03-04
        • 1970-01-01
        相关资源
        最近更新 更多