设置图像选择器sourceType,以便只能选择图片和视频

时间:2015-11-25 00:50:09

标签: ios objective-c uiimagepickercontroller

我希望用户能够使用相机但不能选择拍摄视频。目前,当用户启动相机时,照片和视频控件都显示在视图的左下角。下面是我的launchCamera和imagePickerController方法。

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

}

有没有办法设置图像选择器sourceType,以便只能选择图片和视频?提前谢谢!

2 个答案:

答案 0 :(得分:11)

不要包含此行:

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

来自UIImagePickerController class Reference

  

默认情况下,此属性设置为单值kUTTypeImage,   在捕获媒体时指定静态相机界面,和   指定只应在媒体中显示静止图像   浏览保存的媒体时的选择器。指定电影捕捉   界面,或表示只有电影才能显示   浏览已保存的媒体,使用kUTTypeMovie标识符....

答案 1 :(得分:8)

如果您只想选择图片,请不要将mediaTypes属性设置为给定来源的所有可能媒体类型。

默认情况下,图像选择器仅显示照片库中的图像。

但您mediaTypes的设置会覆盖此默认设置并允许视频。