【问题标题】:UIImagePickerController not picking image in iOS 9UIImagePickerController 没有在 iOS 9 中选择图像
【发布时间】:2015-12-07 11:52:58
【问题描述】:

我正在使用以下代码从相机或照片库中选择图像。在 iOS 8 中,它可以很好地选择图像。但在 iOS 9 中。选择器视图显示但不选择图像。甚至不回到控制器。点击图片什么都不做。我究竟做错了什么。

- (void)showImgaePickerViewForSourceType:(UIImagePickerControllerSourceType)sourceType
{
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
    imagePickerController.sourceType = sourceType;
    imagePickerController.allowsEditing = YES;
    imagePickerController.delegate = self;
    self.imgPickerController = imagePickerController;

    if (IS_IPAD) {
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:self.imgPickerController];
            [popover presentPopoverFromRect:CGRectMake(self.view.frame.size.width/2-200, self.view.frame.size.height/2 - 300, 400, 400) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
            self.popOver = popover;
        }];
    }else{
             [self presentViewController:self.imgPickerController animated:YES completion:NULL];
    }
}

【问题讨论】:

  • 你实现了image picker的委托方法吗?
  • 是的,我说它在 iOS 8 中可以正常工作。
  • 然后向我们展示您对委托方法的实现。他们叫吗?

标签: ios objective-c uiimagepickercontroller


【解决方案1】:

UIPopoverController 在 iOS 9 中已弃用。相反,您应该使用 UIViewController 并将 modalPresentationStyle 设置为 UIModalPresentationPopover

例如:

UIImagePickerController *imagePickerController = ...;
CGRect sourceRect = CGRectMake(self.view.frame.size.width/2-200, self.view.frame.size.height/2 - 300, 400, 400);

imagePickerController.modalPresentationStyle = UIModalPresentationPopover;
[self presentViewController:imagePickerController animated:YES completion:nil]; 
imagePickerController.popoverPresentationController.sourceRect = sourceRect;
imagePickerController.popoverPresentationController.sourceView = self.view;

注意: UIModalPresentationPopover 是在 iOS 8.0 中引入的,如果您需要支持 8.0 之前的版本,那么您需要一些条件检查才能在 iOS 7 和 iOS 8 中使用旧代码+。

注意 2: 我相信上面的技术也足够聪明,可以确定它是否应该在 iPhone 上模态显示选择器而不是弹出框(通过尺寸类)所以我不认为你需要做你的IS_IPAD检查。

【讨论】:

    【解决方案2】:

    我发现我的应用在其他 iPhone(无论是 iOS 8 还是 iOS 9)上运行良好。我无法从任何其他应用程序中选择图像。然后我决定重置我的 iPhone。现在一切都很完美。这不是代码问题。这是我的 iPhone 问题。

    【讨论】:

    • 刚刚遇到与发布的问题完全相同的问题...然后重新启动了我的手机...它成功了! o.O
    【解决方案3】:

    使用此代码在 ios 9 中运行良好。我正在使用操作表显示 2 个选项可供选择。

       -(IBAction)imagepickertapped:(id)sender
       {
           UIActionSheet *popup = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Take Photo" otherButtonTitles:@"Photo Library",nil];
    
           popup.tag =909;
           popup.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
          [popup showInView:self.view];
    
       }
       -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    
        if (actionSheet.tag ==909)
    {
    
        if (buttonIndex == 0)
        {
    
            if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
            {
                UIImagePickerController* picker = [[UIImagePickerController alloc] init];
                picker.sourceType = UIImagePickerControllerSourceTypeCamera;
                // picker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie,kUTTypeImage,nil];
    
                picker.delegate = self;
                [self presentViewController:picker animated:YES completion:NULL];
            }
            else
            {
    
    
                UIAlertView *altnot=[[UIAlertView alloc]initWithTitle:@"Camera Not Available" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                [altnot show];
    
            }
    
        } else if (buttonIndex == 1) {
    
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            //picker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie,kUTTypeImage,nil];
    
            picker.delegate = self;
            picker.editing = YES;
            picker.allowsEditing = YES;
    
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentViewController:picker animated:YES completion:NULL];
        }
    
    }
    
    
     }
    
       - (void)imagePickerController:(UIImagePickerController *)picker
        didFinishPickingImage:(UIImage *)image
                  editingInfo:(NSDictionary *)editingInfo{
    
    
       [picker dismissViewControllerAnimated:YES completion:NULL];
    
    
    imagedata = UIImageJPEGRepresentation(image, 0.3);
    
    
    Userimage.image = image;
    
    
     }
    

    IPAD请参考此链接

    find here

    【讨论】:

      【解决方案4】:

      按照下面的编码

      - (void)imagePicker
      {
      
         UIImagePickerController *picker = [[UIImagePickerController alloc] init];
         picker.delegate = self;
         picker.allowsEditing = YES;
         picker.sourceType = UIImagePickerControllerSourceTypeCamera;
         [self presentViewController:pickerTakePhoto animated:YES completion:nil];
      }
      
      - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
      {
         UIImage *imagePicked = info[UIImagePickerControllerEditedImage];
         picker.delegate = self;
         [picker dismissViewControllerAnimated:YES completion:nil];
      }
      - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 
      {
         [picker dismissViewControllerAnimated:YES completion:NULL];
      }
      

      【讨论】:

        【解决方案5】:

        由于不推荐使用操作表并且需要使用 UIAlertController,因此我正在写一个类似的答案以供新手使用:

            - (IBAction)attachImageTapped:(id)sender
        {
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Attach image" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
            UIAlertAction* pickFromGallery = [UIAlertAction actionWithTitle:@"Take a photo"
                                                                      style:UIAlertActionStyleDefault
                                                                  handler:^(UIAlertAction * action) {
                                                                      if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
                                                                      {
                                                                      UIImagePickerController* picker = [[UIImagePickerController alloc] init];
                                                                      picker.sourceType = UIImagePickerControllerSourceTypeCamera;
                                                                      picker.delegate = self;
                                                                      [self presentViewController:picker animated:YES completion:NULL];
                                                                      }
        
                                                                  }];
            UIAlertAction* takeAPicture = [UIAlertAction actionWithTitle:@"Choose from gallery"
                                                                      style:UIAlertActionStyleDefault
                                                                    handler:^(UIAlertAction * action) {
                                                                        UIImagePickerController* picker = [[UIImagePickerController alloc] init];
                                                                        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                                                                        picker.delegate = self;
                                                                        [self presentViewController:picker animated:YES completion:NULL];
                                                                    }];
            UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel"
                                                                   style:UIAlertActionStyleCancel
                                                                 handler:^(UIAlertAction * action) {
                                                                 }];
        
            [alertController addAction:pickFromGallery];
            [alertController addAction:takeAPicture];
            [alertController addAction:cancel];
            [self presentViewController:alertController animated:YES completion:nil];
        }
        
        -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
        {
            UIImage *imagePicked = info[UIImagePickerControllerOriginalImage];
            [picker dismissViewControllerAnimated:YES completion:nil];
        }
        
        -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
        {
            [picker dismissViewControllerAnimated:YES completion:nil];
        }
        

        【讨论】:

          【解决方案6】:

          请按照以下步骤从库中选择图像,您可以使用“UIAlertController”从相机中获取图像,您只需将以下代码放入您的按钮点击事件中

          - (IBAction)yourButtonClickEvent:(id)sender {
          
          UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Attach image" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
          UIAlertAction* pickFromGallery =
          [UIAlertAction actionWithTitle:@"Take a photo"
                                   style:UIAlertActionStyleDefault
                                 handler:^(UIAlertAction * action) {
                      if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
                          UIImagePickerController* picker = [[UIImagePickerController alloc] init];
                          picker.sourceType = UIImagePickerControllerSourceTypeCamera;
                          picker.delegate = self;
                          [self presentViewController:picker animated:YES completion:NULL];
                                                                      }
          
                                                                  }];
          UIAlertAction* takeAPicture =
          [UIAlertAction actionWithTitle:@"Choose from gallery"
                                   style:UIAlertActionStyleDefault
                                 handler:^(UIAlertAction * action) {
              UIImagePickerController* picker = [[UIImagePickerController alloc] init];
              picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
              picker.delegate = self;
              [self presentViewController:picker animated:YES completion:NULL];
                                                               }];
          UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel"
                              style:UIAlertActionStyleCancel
                            handler:^(UIAlertAction * action) {
                  }];
          
          [alertController addAction:pickFromGallery];
          [alertController addAction:takeAPicture];
          [alertController addAction:cancel];
          [self presentViewController:alertController animated:YES completion:nil];
          

          }

          然后你只要把 UIImagePicker 委托方法如下:

           -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
          
          {
          UIImage *imagePicked = info[UIImagePickerControllerOriginalImage];
          self.imgForUpaloadDocument.image = imagePicked;
          [picker dismissViewControllerAnimated:YES completion:nil];
          
          if(picker.sourceType == UIImagePickerControllerSourceTypeCamera)
          {
              UIImageWriteToSavedPhotosAlbum(imagePicked, nil, nil, nil);
          }
          }
          
          
          -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
            {
              [picker dismissViewControllerAnimated:YES completion:nil];  
            }
          

          【讨论】:

            【解决方案7】:

            使用上面的代码,我想添加一些你可能需要实现 uiimage picker viewcontroller 的东西。

            如果您想防止应用崩溃,您需要说明您访问相机、照片库等的原因。这是 iOS10 中的新功能。

            将以下内容输入到您的 Info.plist 文件中。

            照片

            键:隐私 - 照片库使用说明值:$(PRODUCT_NAME) 照片使用

            相机

            键:隐私 - 相机使用说明值:$(PRODUCT_NAME) 相机使用

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2012-07-24
              • 2016-01-05
              • 1970-01-01
              • 1970-01-01
              • 2023-03-09
              • 2012-10-17
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多