启用UIImagePickerController上的照片库按钮

18

有人知道如何在UIImagePickerController处于相机模式时启用相册按钮吗?就像iPhone上的相机应用程序可以在图像和视频拍摄之间切换,并且还有查看照片库的按钮一样?

5个回答

14

可以通过以下代码实现:

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

2
这不会改变相机控制,而是在相机窗口的顶部添加一个navigationBar。这可能是你想要的,也可能不是,但我认为这不是OP所想要的。 - DaGaMs
2
很棒的解决方案。只需记得检查相机源是否真正可用。类似这样: if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { self.picker.sourceType = UIImagePickerControllerSourceTypeCamera; } - EsbenB
问题在于控件只出现了一会儿 - 我在函数上设置了断点 - 当 UiImagePickerController 显示时,它被视图覆盖得像毛毛虫一样。 - Fabrizio Bartolomucci

7

3

1

@epsilontik 代码的 Swift2 版本:

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

1
我已经对苹果的PhotoPicker示例应用进行了微调。我删除了所有相机控件并添加了自己的按钮。单击后,UIImagePickerControllerSourceType设置为UIImagePickerControllerSourceTypePhotoLibrary。
对我来说棘手的部分是在选择图像后“解除”(可能是技术上错误的词)照片库。我通过将源类型设置回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

抱歉打扰你,但如果你还有代码的话,我会非常感兴趣! - bdv
嗨bdv。我找到了我的代码并将其发布到了我的原始答案中。我不记得我是如何做到的,因为我已经两年没有在这个应用程序上工作了,但我是从苹果的原始PhotoPicker应用程序开始的。https://developer.apple.com/library/ios/samplecode/PhotoPicker/Introduction/Intro.html - ganime

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接