how to add image from photo library or camera to my Ipad application?

后端 未结 3 1784
北海茫月
北海茫月 2021-01-15 13:28

I want to add images from photo library or by using camera to my ipad application. I had used same coding as iphone . But the application is crashing. I think this is not t

相关标签:
3条回答
  • 2021-01-15 13:50

    The answer is in your error message.

    Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'On iPad, UIImagePickerController must be presented via UIPopoverController'
    

    On an iPad, you can't just use the iPhone method of selecting an image. You need to use a UIPopoverController, you can pass it your UIImagePickerController with:

    UIPopoverController* popover = [[UIPopoverController alloc] initWithContentViewController: imagePicker];

    Then implement the UIPopoverControllerDelegate protocol in your class.

    You can find an example of this here.

    0 讨论(0)
  • 2021-01-15 13:52

    for Swift 4

    get image from camera

    let picker = UIImagePickerController()
    picker.delegate = self
    picker.allowsEditing = true
    picker.sourceType = .camera
    self.present(picker, animated: true, completion: nil)
    

    get image from library

    let picker = UIImagePickerController()
    picker.delegate = self
    picker.allowsEditing = true
    picker.sourceType = .photoLibrary
    self.present(picker, animated: true, completion: nil)
    

    delegate function

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
      var result = false
    
      if let image = info[UIImagePickerControllerEditedImage] as? UIImage {
          //do something to image here ...
    
          picker.dismiss(animated: true, completion: nil)
      }
    }
    

    In your view controller, don't forget to inherit class UIImagePickerControllerDelegate, UINavigationControllerDelegate

    0 讨论(0)
  • 2021-01-15 14:13
    - (void)buttonTap {
    
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle: nil
                                                             delegate: self
                                                    cancelButtonTitle: @"Cancel"
                                               destructiveButtonTitle: nil
                                                    otherButtonTitles: @"Take a new photo",
                                                             @"Choose from existing", nil];
                                                       [actionSheet showInView:self.view];
    }
    
    - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    
        int i = buttonIndex;
    
            switch(i) {
    
        case 0:
        {
    
          //Code for camera  
    
        }
            break;
        case 1:
        {
            UIImagePickerController * picker = [[UIImagePickerController alloc] init];
            picker.delegate = self;
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
           self.popover = [[UIPopoverController alloc] initWithContentViewController: picker];
            self.popover.delegate =self;
            [self.popover presentPopoverFromRect:self.view.bounds inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
        }
    
        default:
            // Do Nothing.........
            break;
    
        }
    }
    
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    
        NSLog(@"Picker returned successfully.");
    
        UIImage *selectedImage;
    
        NSURL *mediaUrl;
    
        mediaUrl = (NSURL *)[info valueForKey:UIImagePickerControllerMediaURL];
    
        if (mediaUrl == nil) {
    
        selectedImage = (UIImage *) [info valueForKey:UIImagePickerControllerEditedImage];
        if (selectedImage == nil) {
    
            selectedImage= (UIImage *) [info valueForKey:UIImagePickerControllerOriginalImage];
            NSLog(@"Original image picked.");
    
             }
        else {
    
            NSLog(@"Edited image picked.");
    
             }    
    
        }
    
        [picker dismissModalViewControllerAnimated:YES];
    
    
        [self.imageButton setImage:selectedImage forState:UIControlStateNormal];
    }
    
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    
        [self dismissModalViewControllerAnimated:YES];
    }
    
    0 讨论(0)
提交回复
热议问题