UIImagePickerController 在 iOS 设备上工作但不在 iOS 模拟器上工作?

Posted

技术标签:

【中文标题】UIImagePickerController 在 iOS 设备上工作但不在 iOS 模拟器上工作?【英文标题】:UIImagePickerController working on iOS device but not on iOS simulator? 【发布时间】:2014-06-18 07:51:34 【问题描述】:

我已经在物理 ios 设备上运行我的应用程序一段时间了,没有任何问题。但是现在 UIImagePickerController 视图不会出现在模拟器上。我已经使用here 方法将照片保存到模拟器中,并确认它们确实存在于模拟器中的图像库中。 Xcode 中没有弹出错误。而且我尝试过使用不同的源类型,但无济于事。知道我可能做错了什么吗?非常感谢!

代码

    UIImagePickerControllerSourceType sourceType;
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
            
                sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            
            else
            
                sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            
            //Display photo library.
            [self startImagePickerControllerFromViewController: self
                                            usingDelegate: self
                                           withSourceType: sourceType];

...

- (BOOL)startImagePickerControllerFromViewController:(UIViewController*) controller
                                  usingDelegate:(id <UIImagePickerControllerDelegate,
                                                     UINavigationControllerDelegate>) delegate
                                 withSourceType:(UIImagePickerControllerSourceType) sourceType

    //Insure camera, controller, and delegate exist.
    if (([UIImagePickerController isSourceTypeAvailable:
          UIImagePickerControllerSourceTypeCamera] == NO)
        || (delegate == nil)
        || (controller == nil))
        return NO;

    //Create the ImagePicker.
    UIImagePickerController *imagePickerUI = [[UIImagePickerController alloc] init];
    imagePickerUI.sourceType = sourceType;
    //Only allow still images.
    imagePickerUI.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeImage, nil];
    //Turn off editing.
    imagePickerUI.allowsEditing = NO;
    //Set the delegate.
    imagePickerUI.delegate = delegate;
    //Present the picker view.
    [controller presentViewController:imagePickerUI animated:YES completion:nil];

    return YES;

【问题讨论】:

【参考方案1】:
  if (([UIImagePickerController isSourceTypeAvailable:
      UIImagePickerControllerSourceTypeCamera] == NO)

以上条件保证模拟器无法运行,因为模拟器没有摄像头。

【讨论】:

以上是关于UIImagePickerController 在 iOS 设备上工作但不在 iOS 模拟器上工作?的主要内容,如果未能解决你的问题,请参考以下文章

在 iOS 7 中呈现 UIImagePickerController 的问题

如何在 iPad 上关闭 UIImagePickerController

仅在横向 UIImagePickerController 中显示CameraControls

UIImagePickerController 在 iOS 11 上泄漏

UIImagePickerController 呈现在视图中

在 iOS 7 上显示 UIImagePickerController 会导致崩溃