iOS应用程序 - 尝试从图库中获取iOS照片但收到错误

时间:2015-04-30 06:47:21

标签: ios iphone ipad

我是iOS编码的新手,所以我提前道歉。我知道这个主题已被多次覆盖(我搜索过)但是我似乎无法解决我的问题,这就是我发帖的原因。

我正在尝试访问iOS照片库,但我不断收到两个错误:

一个是 '应用程序试图在目标上呈现一个零模态视图控制器。

编辑:通过在ChooseExsisting中启动_picker来修复上述错误,如评论中所建议的那样。

另一个

[CameraController ChooseExsiting:]:无法识别的选择器发送到实例0x157e11330'

我的代码如下:

- (IBAction)ChooseExsiting {
UIImagePickerController *pickerController = [[UIImagePickerController alloc]
                                             init];
pickerController.delegate = self;
[self.picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:_picker animated:YES completion:NULL]; }

我想我的ChooseExsisting代码不正确。有人会有什么建议吗?我将不胜感激。

4 个答案:

答案 0 :(得分:1)

在ChooseExisting方法中,将控制器实例化为局部变量,然后使用_picker属性变量调用present,该变量可能为nil。要么从局部变量中呈现控制器,要么像在TakePhoto方法中那样初始化属性。

编辑:至于第二部分,如果你将它们连接到故事板中的Tap处理程序,那么你的IBActions都有一个错误的方法签名。他们应该是这样的:

a.inject(:merge!) #=> {:a=>:b, :c=>:d}

- (IBAction) TakePhoto:(id)sender

答案 1 :(得分:0)

将ChooseExisitng方法更改为以下方法:

- (IBAction)ChooseExsiting:(id) sender {
    UIImagePickerController *pickerController = [[UIImagePickerController alloc]
                                         init];
    pickerController.delegate = self;
    [pickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    [self presentViewController:pickerController animated:YES completion:NULL]; }

- (IBAction)ChooseExsiting:(id) sender {
    self.picker = [[UIImagePickerController alloc]
                                         init];
    self.picker.delegate = self;
    [self.picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    [self presentViewController:self.picker animated:YES completion:NULL]; }

答案 2 :(得分:0)

 You can try this . It work for me
 Follow below three steps1.First of all you need to add below delegate in .h file (UIImagePickerControllerDelegate,UIPickerViewDelegate)
 2.Add below three methods in your controller
 3. On button click call below methods
 here choose photo for open gallary. so on button click call choose photo method
 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
   CGSize newSize=CGSizeMake(150, 150);
   UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [[info objectForKey:UIImagePickerControllerEditedImage] drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();
   img_profile_pic.image = newImage;
   [self dismissViewControllerAnimated:YES completion:nil];
   isProfilePic=TRUE;
}
- (void)showImagePickerForSourceType:(UIImagePickerControllerSourceType)sourceType
{
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.allowsEditing = YES;
    imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
    imagePickerController.sourceType = sourceType;
    imagePickerController.delegate = self;
    imagePickerController = imagePickerController;
    [self presentViewController:imagePickerController animated:YES completion:nil];
 }

  - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
  {
      [self dismissViewControllerAnimated:YES completion:nil];
   }


  -(void) takePhoto{
       [self showImagePickerForSourceType:UIImagePickerControllerSourceTypeCamera];
   }
  -(void) choosePhoto{
     [self showImagePickerForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
   }

答案 3 :(得分:0)

而不是做



    if ([UIImagePickerController isSourceTypeAvailable:!UIImagePickerControllerSourceTypeCamera]) {

    self.picker = [[UIImagePickerController alloc] init];
    self.picker.delegate = self;
    [self.picker setSourceType:UIImagePickerControllerSourceTypeCamera];
    [self presentViewController:_picker animated:YES completion:NULL];
    } }

你应该这样做



    if ([UIImagePickerController isSourceTypeAvailable:!UIImagePickerControllerSourceTypeCamera]) {
    if(!_picker){
    _picker = [[UIImagePickerController alloc] init];
    _picker.delegate = self;
    [_picker setSourceType:UIImagePickerControllerSourceTypeCamera];
    [self presentViewController:_picker animated:YES completion:NULL];

    }else{

    [self presentViewController:_picker animated:YES completion:NULL];

    }}}

您应该在呈现之前初始化_picker属性,现在您正在尝试初始化VC的本地属性,或者您也可以在代码中使用self.picker而不是_picker。