【问题标题】:how to open an image picker on ipad just in code如何仅在代码中打开 ipad 上的图像选择器
【发布时间】:2013-01-22 21:25:16
【问题描述】:

如何在代码中创建图像选择器?

我在 ipad 上使用 iOS 6.0 和 ARC。

我希望能够选择图片并以某种方式获取所选图片的 UIImage。

我确实添加了代表: 在这里输入代码

在 viewDidLoad 方法中做了 enter code hereimagePicker = [[UIImagePickerController alloc] init];

在按钮方法中我已经放了

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

崩溃发生在

[self presentModalViewController:imagePicker animated:YES];

【问题讨论】:

  • 我已经更新了这个问题来解释更多。
  • 如果您从图库中选择照片,则无法在 iPad 上以模态方式呈现此内容。您需要在 UIPopoverController 中显示选择器。
  • 实际上,您的代码对我有用。您的问题可能出在其他地方。
  • @user1157605 你在 iPhone 上测试过,不是吗。
  • @0x7fffffff 啊,是的,直到现在才看到。

标签: ios objective-c ipad uiimage


【解决方案1】:
【解决方案2】:

我的一个项目中有类似的代码,但我有

[self presentViewController:imagePicker animated:YES completion:nil];

而不是

[self presentModalViewController:imagePicker animated:YES];

试试看它是否有效。注意 presentViewController 而不是 presentModalViewController。

【讨论】:

  • 更改为 [self presentViewController:imagePicker animated:YES completion:nil];没有帮助它像以前一样崩溃。
  • 是的,这是因为我的代码是针对 iphone 的,而您使用的是 ipad。对于 ipad 不确定。
【解决方案3】:

.h中添加属性

@property (strong) UIPopoverController *pop;

在您的按钮实现下的.m 文件中添加如下内容:

if (self.pop) {
        [self.pop dismissPopoverAnimated:YES];

    }


UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
       imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
       imagePickerController.delegate = self;
       imagePickerController.allowsEditing = YES; //if you want to edit the image

       self.pop=[[UIPopoverController alloc] initWithContentViewController:imagePickerController];
       //choose the direction of the arrow for the popover
       [self.pop presentPopoverFromRect:((UIButton *)sender).bounds inView:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
  }
}

确保您在 .h 中设置了 <UIPopoverControllerDelegate> 委托

【讨论】:

  • 我在 .h @interface RayTracerViewController 中有: UIViewController { UIImagePickerController *imagePicker; } 比在 .m 中我有: self.buttonOne = [UIButton buttonWithType:UIButtonTypeRoundedRect]; self.buttonOne.frame = CGRectMake(320.0, 20.0, 300.0, 40.0); [self.buttonOne setTitle:@"off gumb" forState:UIControlStateNormal]; [self.buttonOne addTarget:self action:@selector(onButtonPressed1:) forControlEvents:UIControlEventTouchUpInside]; self.view addSubview:self.buttonOne];现在我确实写了:
  • UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; imagePickerController.delegate = self; imagePickerController.allowsEditing = YES; //如果要编辑图片 self.pop=[[UIPopoverController alloc] initWithContentViewController:imagePickerController]; [self.pop presentPopoverFromRect:self.buttonOne.bounds inView:self.buttonOne allowedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
  • Grapher2[54097:11303] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[UIPopoverController presentPopoverFromRect:inView:permittedArrowDirections:animated:]:无法从视图中呈现弹出框没有窗户。'
  • 首先,如果使用 iPad 添加 ,您的帖子中似乎缺少该内容
  • 我现在做了同样的崩溃。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-05
  • 1970-01-01
  • 1970-01-01
  • 2015-07-11
相关资源
最近更新 更多