Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Image picker sourceType so that only pictures and no video can be selected

I want users to be able to use a camera but not have the option of taking a video. As it stands, both photo and video controls show in the bottom left corner of the view when camera is launched by user. Below are my launchCamera and imagePickerController methods.

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

self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
self.challengePic.image = self.image;
if (self.imagePicker.sourceType == UIImagePickerControllerSourceTypeCamera) {
    UIImageWriteToSavedPhotosAlbum(self.image, nil, nil, nil);
    self.challengePic.image = self.image;

    }

[self dismissViewControllerAnimated:YES completion:nil];
}


- (IBAction)launchCamera:(id)sender {
if (self.image == nil && [self.videoFilePath length] == 0) {
    self.imagePicker = [[UIImagePickerController alloc] init];
    self.imagePicker.delegate = self;
    self.imagePicker.allowsEditing = NO;

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    }
    else {
        self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    self.imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:self.imagePicker.sourceType];

    [self presentViewController:self.imagePicker animated:NO completion:nil];
}

}

- (IBAction)launchAlbum:(id)sender {
if (self.image == nil && [self.videoFilePath length] == 0) {
    self.imagePicker = [[UIImagePickerController alloc] init];
    self.imagePicker.delegate = self;
    self.imagePicker.allowsEditing = NO;

    self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    self.imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:self.imagePicker.sourceType];

    [self presentViewController:self.imagePicker animated:NO completion:nil];
}

}

Is there any way of setting the Image picker sourceType so that only pictures and no video can be selected? Thanks in advance!

like image 509
Bryan Campbell Avatar asked Nov 25 '15 00:11

Bryan Campbell


People also ask

What is photo picker?

The photo picker provides a browsable, searchable interface that presents the user with their media library, sorted by date from newest to oldest. This tool provides a safe, built-in way for users to select images and videos, without needing to grant your app access to their entire media library.

What is Iphone photo picker?

The system Photos picker is the best way for most apps to access photos and videos on iOS. The picker runs out of process, so your app doesn't need to request any library access to use it. It has an intuitive UI and an easy-to-use API.

How do I use image picker in Swift?

Once you have a brand new project, you can go to the Main. storyboard file and add two elements inside the view controller. The first one is a button, which will show up an image picker modal whenever it gets pressed. The second one is an image view.


2 Answers

Don't include this line:

self.imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:self.imagePicker.sourceType];

From the UIImagePickerController class Reference:

By default, this property is set to the single value kUTTypeImage, which designates the still camera interface when capturing media, and specifies that only still images should be displayed in the media picker when browsing saved media. To designate the movie capture interface, or to indicate that only movies should be displayed when browsing saved media, use the kUTTypeMovie identifier….

like image 93
beyowulf Avatar answered Nov 01 '22 00:11

beyowulf


If you only want to select images, don't set the mediaTypes property to all possible media types for the given source.

By default, the image picker will only show images from the photo library.

But your setting of mediaTypes is overriding this default and allowing videos as well.

like image 36
rmaddy Avatar answered Oct 31 '22 23:10

rmaddy