简体   繁体   English

iOS应用程序-试图从图库中获取iOS照片但出现错误

[英]iOS App - Trying to Get iOS photo from gallery but getting an error

I'm new to iOS coding, so my apologies in advance. 我是iOS编码的新手,所以提前致歉。 I know this topic has been covered many times (I searched) however I can't seem to resolve my problem, which is why I am posting. 我知道这个话题已经被讨论过很多次了(我已经搜索过),但是我似乎无法解决我的问题,这就是为什么我要发布。

I am trying to access the iOS photo gallery but I keep getting two errors: 我正在尝试访问iOS照片库,但仍然出现两个错误:

One is 'Application tried to present a nil modal view controller on target .' 一种是“应用程序试图在目标上展示一个无模式视图控制器”。

Edit: The above error was fixed by initing the _picker in ChooseExsisting, as was suggested in the comments. 编辑:上面的错误是通过在ChooseExsisting中初始化_picker来解决的,如注释中所建议。

The other 另一个

[CameraController ChooseExsiting:]: unrecognized selector sent to instance 0x157e11330' [CameraController ChooseExsiting:]:无法识别的选择器已发送到实例0x157e11330'

My code is as follows: 我的代码如下:

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

I imagine my ChooseExsisting code is incorrect. 我认为我的ChooseExsisting代码不正确。 Would anyone have any suggestions? 有人有什么建议吗? I would greatly appreciate it. 我将不胜感激。

In your ChooseExisting method, you instantiate a controller into a local variable, but then call present with _picker property variable which is probably nil. 在ChooseExisting方法中,将控制器实例化为局部变量,然后使用_picker属性变量调用present,该变量可能为nil。 Either present the controller from the local variable or init the property like in TakePhoto method. 从局部变量显示控制器,或者像TakePhoto方法一样初始化属性。

EDIT: As for the second part, both your IBActions have a wrong method signature if you are connecting them to Tap handlers in the Storyboard for example. 编辑:至于第二部分,例如,如果要将两个IBAction连接到Storyboard中的Tap处理程序,则它们的方法签名错误。 They should look like this: 它们应如下所示:

- (IBAction) TakePhoto:(id)sender

- (IBAction) ChooseExsiting:(id)sender

Change your ChooseExisitng method to below one: 将您的ChooseExisitng方法更改为以下一种:

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

or 要么

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

Instead of doing 而不是做



    if ([UIImagePickerController isSourceTypeAvailable:!UIImagePickerControllerSourceTypeCamera]) {

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

You should do this 你应该做这个



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

    }}}

You should initialize _picker property before presenting it, right now you're trying to initialize local property of VC or you can also present self.picker instead of _picker in your code. 您应该在提供_picker属性之前对其进行初始化,现在,您正在尝试初始化VC的本地属性,或者还可以在代码中提供self.picker而不是_picker。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM