18

Does anyone know how to enable the photo album button on the UIImagePickerController when its in the camera mode? Like how the camera app on on the iphone can toggle between image and video taking and also has the button to view the photo library?

palmsey
  • 5,812
  • 3
  • 37
  • 41
MBU
  • 4,998
  • 12
  • 59
  • 98

5 Answers5

14

This can be done via the following lines:

- (void) navigationController: (UINavigationController *) navigationController  willShowViewController: (UIViewController *) viewController animated: (BOOL) animated {
    if (imagePickerController.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
        UIBarButtonItem* button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(showCamera:)];
        viewController.navigationItem.rightBarButtonItems = [NSArray arrayWithObject:button];
    } else {
        UIBarButtonItem* button = [[UIBarButtonItem alloc] initWithTitle:@"Library" style:UIBarButtonItemStylePlain target:self action:@selector(showLibrary:)];
        viewController.navigationItem.leftBarButtonItems = [NSArray arrayWithObject:button];
        viewController.navigationItem.title = @"Take Photo";
        viewController.navigationController.navigationBarHidden = NO; // important
    }
}

- (void) showCamera: (id) sender {
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
}

- (void) showLibrary: (id) sender {
    imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
epsilontik
  • 141
  • 1
  • 2
  • 2
    This will not change the camera control, but instead add a navigationBar into the top part of the camera window. That might or might not be what you want, but I don't think it's what the OP had in mind. – DaGaMs Jun 26 '12 at 07:17
  • 2
    great solution. Just remember to check if the camera source is really available. Something like: if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { self.picker.sourceType = UIImagePickerControllerSourceTypeCamera; } – EsbenB May 28 '13 at 13:26
  • The problem is that the control appears for some moments - I put a breakpoint on the function - to be covered as a caterpillar by the view of the UiImagePickerController when it is shown. – Fabrizio Bartolomucci Nov 15 '19 at 10:35
7

I'm afraid that this can't be done in that easy way (just enable or disable some magic feature). For some simple requirement, you can use cameraOverlayView and showsCameraControls to make the implementation.

If you want to switch between photo/video mode, i think you can check this demo: http://developer.apple.com/library/ios/#samplecode/AVCam/Introduction/Intro.html

Kevin Xue
  • 756
  • 6
  • 8
3

Here is an example app and very simple library that lets you get the Take Photo or Choose from Library just like Facebook does. https://github.com/fulldecent/FDTake

William Entriken
  • 37,208
  • 23
  • 149
  • 195
1

I have done this tweaking Apple's PhotoPicker example app. I have removed all the camera controls and added my own button. When clicked, UIImagePickerControllerSourceType is set to the UIImagePickerControllerSourceTypePhotoLibrary.

The tricky part for me was "dismissing" (might be technically the wrong word) the Photo Library after the image was picked. I did this by setting the source type back to UIImagePickerControllerSourceTypeCamera. This brings back the camera overlay view.

ViewController.h

#import <UIKit/UIKit.h>
#import <CoreGraphics/CoreGraphics.h>
#import <ImageIO/ImageIO.h>


@interface ViewController : UIViewController <UIImagePickerControllerDelegate> {

//

}

@property (nonatomic, strong) UIImagePickerController *imagePicker;
- (IBAction)uploadNewPhotoTapped:(id)sender;
@end


ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

 //Other code

- (IBAction)uploadNewPhotoTapped:(id)sender {

    UIImagePickerController *imagePickController=[[UIImagePickerController alloc]init];
    //You can use isSourceTypeAvailable to check

    if ([UIImagePickerController    isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        imagePickController.sourceType=UIImagePickerControllerSourceTypeCamera;
        imagePickController.showsCameraControls=YES;
        //  self.usingPopover = NO;
    }
    else if ([UIImagePickerController  isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {//Check PhotoLibrary  available or not
        imagePickController.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
        imagePickController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    else if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) //Check front Camera available or not
        imagePickController.sourceType=UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    //else //!!!!!!!!!!!exception

    imagePickController.delegate=self;
    imagePickController.allowsEditing=NO;

    [self presentModalViewController:imagePickController animated:YES];
}


- (void)imagePickerController:(UIImagePickerController *)picker  didFinishPickingMediaWithInfo:(NSDictionary *)info {

    UIImage *originalImage=[info objectForKey:UIImagePickerControllerOriginalImage];

    //Do whatever with your image   
    NSData *data = UIImageJPEGRepresentation (
                                          originalImage,
                                          1.0
                                          );

    [self dismissModalViewControllerAnimated:YES];
}

   // Other code
   @end
ganime
  • 129
  • 2
  • 9
  • Sorry to awake a zombie here, but if you still have your code, I'd be really interested! – bdv Dec 10 '14 at 05:13
  • Hi bdv. I actually dug up my code and posted it to my original answer. I don't remember exactly how I did it as I haven't worked on this app in 2 years but I started with Apple's original PhotoPicker App. https://developer.apple.com/library/ios/samplecode/PhotoPicker/Introduction/Intro.html – ganime Dec 11 '14 at 14:48
1

Swift2 version of @epsilontik code:

    //mediaPicker is your UIImagePickerController
func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
    if(mediaPicker.sourceType == UIImagePickerControllerSourceType.PhotoLibrary){
        let button = UIBarButtonItem(title: "Take picture", style: UIBarButtonItemStyle.Plain, target: self, action: "showCamera")
        viewController.navigationItem.rightBarButtonItem = button
    }else{
        let button = UIBarButtonItem(title: "Choose picture", style: UIBarButtonItemStyle.Plain, target: self, action: "choosePicture")
        viewController.navigationItem.rightBarButtonItem = button
        viewController.navigationController?.navigationBarHidden = false
        viewController.navigationController?.navigationBar.translucent = true
    }
}

func showCamera(){
    mediaPicker.sourceType = UIImagePickerControllerSourceType.Camera
}

func choosePicture(){
    mediaPicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
}
Azephiar
  • 501
  • 6
  • 19