首页 文章

启用UIImagePickerController上的照片库按钮

提问于
浏览
18

有没有人知道如何在相机模式下启用UIImagePickerController上的相册按钮?就像iphone上的相机应用程序如何在图像和视频拍摄之间切换一样,还有按钮来查看照片库?

5 回答

  • 14

    我可以用这种简单的方式完成(只需启用或禁用某些魔术功能) . 对于一些简单的要求,您可以使用cameraOverlayViewshowsCameraControls来实现 .

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

  • 1

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

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

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

  • 1

    我做了这个调整Apple的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
    
  • 7

    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
    }
    

相关问题