【问题标题】:How to use UIImagePickerController in iPad?如何在 iPad 中使用 UIImagePickerController?
【发布时间】:2012-01-26 07:46:40
【问题描述】:

您好,我正在开发一个通用应用程序 (iPhone/iPad)。一个功能是我必须从相册中选择一张照片并将其显示在 UIImageView 上。

现在的问题是它在 iPhone 上运行良好,但是当我尝试打开相册时它崩溃了。我在操作表委托中的代码是这样的:

- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
        if ( ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]))

        {   
            if (buttonIndex == 0)
            {
                [self lockAllImagesOnTheScreen];
                imagePicker.sourceType=UIImagePickerControllerSourceTypeCamera;
                [self presentModalViewController:imagePicker animated:YES];
            }
            if (buttonIndex == 1)
            {
                [self lockAllImagesOnTheScreen];

                imagePicker.sourceType= UIImagePickerControllerSourceTypePhotoLibrary;
                [self presentModalViewController:imagePicker animated:YES];
            }

        }
        else {

            if (buttonIndex == 0)
            {
                [self lockAllImagesOnTheScreen];
                imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
                imagePicker.sourceType= UIImagePickerControllerSourceTypePhotoLibrary;
                [self presentModalViewController:imagePicker animated:YES];
            }
        }



    }

    else{
        if ( ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]))

        {   
            if (buttonIndex == 0)
            {
                [self lockAllImagesOnTheScreen];
                imagePicker.sourceType=UIImagePickerControllerSourceTypeCamera;
                [self presentModalViewController:imagePicker animated:YES];
            }
            if (buttonIndex == 1)
            {
                [self lockAllImagesOnTheScreen];

                imagePicker.sourceType= UIImagePickerControllerSourceTypePhotoLibrary;
                [self presentModalViewController:imagePicker animated:YES];
            }

        }
        else {

            if (buttonIndex == 0)
            {
                [self lockAllImagesOnTheScreen];
                imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
                imagePicker.sourceType= UIImagePickerControllerSourceTypePhotoLibrary;
                [self presentModalViewController:imagePicker animated:YES];
            }
        }


    }


}

任何人都可以帮助我吗?我已经检查了 stackOverflow 并用谷歌搜索了它,但徒劳无功。

【问题讨论】:

  • 如果有人献身,那么请也写下它的原因,因为我已经尝试过第一次弄清楚但没有任何帮助,这就是我在这里问这个问题的原因
  • 有什么异常?如果是内存,你试过 NSZombiesEnabled 吗?
  • "程序收到信号 SIGABRT" 这是异常
  • NSZombies 没有帮助
  • 对编辑标题或添加崩溃有兴趣吗?

标签: iphone ipad uiimagepickercontroller


【解决方案1】:

UIImagePickerController必须在 iPad 上显示为UIPopoverController

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];
}

编辑:为UIPopoverController添加一个强属性:

@property (nonatomic, strong) UIPopoverController *popOver;

应该在委托方法中关闭弹出框:

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

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 

【讨论】:

  • 这个答案不完整。 self.popOver 是什么?提供的代码中没有它的定义
  • 这不是那么不完整,您只需要在字里行间阅读...显然 self.popOver 是UIPopoverController 类型的属性,大概用于处理稍后关闭弹出窗口。只是说'
  • 不,不完整。如果我们应该“阅读字里行间”,那么我们至少需要一个“阅读”的方向。这是一个学习论坛。恭喜您的 iO 体验,但无需粗鲁。
  • 我还要添加关于在 -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker -(void)image:(UIImage *)image finishedSavingWithError:(NSError *)error contextInfo:(void *)contextInfo 中关闭弹出框的注释
  • 在这段代码中...... self.selectedImageView 是什么?我正在尝试做同样的事情,但由于某种原因,相机被限制在一个非常小的窗口中。 (我怀疑这是因为我误解了 selectedImageView 应该是什么......)
【解决方案2】:

我在这里向您展示 SWIFT 方式:

import UIKit
class StoreItemViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate
{
    @IBOutlet weak var button: UIButton!
    @IBOutlet weak var productImage: UIImageView!
    var popOver:UIPopoverController?

    @IBAction func buttonSelected(sender:UIButton)
    {
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum)
        {
            var imagePickerController = UIImagePickerController()
            imagePickerController.delegate = self
            imagePickerController.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
            imagePickerController.allowsEditing = false

            if UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad
            {
                self.popOver = UIPopoverController(contentViewController: imagePickerController)
                self.popOver?.presentPopoverFromRect(self.productImage.bounds, inView: self.productImage, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)      
            }
            else
            {
                self.presentViewController(imagePickerController, animated: true, completion: { imageP in

                })
            } 
        }
    }

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
         //do anything with the image
        let selectedImage = info[UIImagePickerControllerOriginalImage] as UIImage

        //closing the popup
        popOver?.dismissPopoverAnimated(true)

    }

    func imagePickerControllerDidCancel(picker: UIImagePickerController) 
    {
         println("cancel")

       //closing the popup
       popOver?.dismissPopoverAnimated(true)
    }
}

【讨论】:

    【解决方案3】:

    Apple 文档说

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

    这与它的行为完全相反?!?你不能从弹出窗口中呈现UIImagePickerControllerSourceTypeCamera,你不能以模态方式呈现UIImagePickerControllerSourceTypePhotoLibraryUIImagePickerControllerSourceTypeSavedPhotosAlbum

    奇怪...

    【讨论】:

    • 你能提供一个链接吗?我可以找到的 Apple 文档说相反:您必须对媒体浏览器使用弹出框...“在 iPad 上,如果您指定 UIImagePickerController.SourceType.camera 的源类型,则可以模态显示图像选择器(全屏)或使用弹出框。但是,Apple 建议您仅全屏显示相机界面。 developer.apple.com/documentation/uikit/uiimagepickercontroller
    【解决方案4】:

    发布 iOS 8: 尝试在

    中添加 popOver 控制器

    [[NSOperationQueue mainQueue] addOperationWithBlock:^{ }];

    原因: 这是因为在 iOS 8 中,警报视图和操作表实际上是呈现的视图控制器 (UIAlertController)。因此,如果您正在呈现一个新的视图控制器以响应来自 UIAlertView 的操作,那么它会在 UIAlertController 被解除时呈现。您需要在主队列中执行此操作而不会干扰导航。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-26
      • 2011-10-06
      相关资源
      最近更新 更多