启用UIImagePickerController上的照片库button

有没有人知道如何启用相机模式下的UIImagePickerController相册button? 就像iPhone上的相机应用程序可以在图像和video拍摄之间切换,还有button来查看照片库?

恐怕这不能以简单的方式完成(只是启用或禁用一些魔术function)。 对于一些简单的要求,你可以使用cameraOverlayView和showsCameraControls来实现。

如果你想切换照片/video模式,我想你可以检查这个演示: http : //developer.apple.com/library/ios/#samplecode/AVCam/Introduction/Intro.html

这可以通过以下几行完成:

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

下面是一个示例应用程序和非常简单的库,可以让您像Facebook那样获取拍照或从库中select。 https://github.com/fulldecent/FDTake

我已经做了这个调整苹果的PhotoPicker示例应用程序。 我已经删除了所有的相机控制,并添加了我自己的button。 单击时,UIImagePickerControllerSourceType被设置为UIImagePickerControllerSourceTypePhotoLibrary。

对于我来说,棘手的部分是在图像被选中后“解散”(可能在技术上是错误的词)图片库。 我通过设置源types返回到UIImagePickerControllerSourceTypeCamera。 这将带回相机重叠视图。

 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 

Swift2版本的@epsilontik代码:

  //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 }