iDevice 相机显示黑色而不是预览 [英] iDevice camera shows black instead of preview

查看:28
本文介绍了iDevice 相机显示黑色而不是预览的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个从 iDevice 的相机捕捉图像并将其上传到网络服务的应用程序.

I am developing an app that captures images from iDevice's camera and upload it to web service.

没问题,除了设备的摄像头,一切正常.设备的相机让我发疯.我使用下面的代码来允许用户捕获图像.有时相机显示预览,有时不显示.而不是预览只是在屏幕上显示完全黑暗.如果我从后置摄像头切换到前置摄像头开始工作正常.我什至尝试从设备中删除所有后台应用程序并尽可能多地清除内存;仍然没有运气,我被卡住了.:(

- (IBAction)addNewImage:(id)sender
{
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        // Take picture from camera
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;

        // set no to take as much pictures as user want.
        imagePicker.showsCameraControls = YES;

        // Show user the camera
        [self presentModalViewController:imagePicker
                                animated:YES];
    }
    else
    {
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentModalViewController:imagePicker
                                animated:YES];        
    }    
}

推荐答案

我在我的应用中遇到过这个问题.虽然我从来没有发现问题是什么,但我重写了我的代码来定义一个 UIIMagePickerController 类型的属性并在 getter 中初始化它一次.使用此属性初始化相机视图:

I had faced this issue in my app. Though I never found out the what the issue was, I rewrote my code to define a property of UIIMagePickerController type and initialize it once in the getter. Used this property to initialize the camera view :

吸气剂:

-(UIImagePickerController *) imagePicker{
    if(!_imagePicker){
       _imagePicker = [[UIImagePickerController alloc] init];
       _imagePicker.delegate = self;
       if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
           _imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
       }
       else{
           _imagePicker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
       }  

   }
    return _imagePicker;
}


- (IBAction)addNewImage:(id)sender{
  if (self.imagePicker)
  {
      [self presentViewController:self.imagePicker animated:YES completion:^{}];
  }
}

出于某种原因,这解决了预览有时显示黑屏的问题

For some reason this got rid of the issue with preview sometimes showing a black screen

这篇关于iDevice 相机显示黑色而不是预览的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆