【问题标题】:Enabling the photo library button on the UIImagePickerController启用 UIImagePickerController 上的照片库按钮
【发布时间】:2011-12-16 01:41:06
【问题描述】:

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

【问题讨论】:

    标签: ios uiimagepickercontroller iphone-4


    【解决方案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;
    }
    

    【讨论】:

    • 这不会改变相机控制,而是在相机窗口的顶部添加一个导航栏。这可能是也可能不是你想要的,但我认为这不是 OP 的想法。
    • 很好的解决方案。只要记住检查相机源是否真的可用。类似于: if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { self.picker.sourceType = UIImagePickerControllerSourceTypeCamera; }
    • 问题是控件出现了一段时间——我在函数上放了一个断点——当它显示时被 UiImagePickerController 的视图覆盖为毛毛虫。
    【解决方案2】:

    恐怕这不能以那么简单的方式完成(只需启用或禁用一些神奇的功能)。对于一些简单的需求,可以使用cameraOverlayViewshowsCameraControls来实现。

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        我已经对 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
        

        【讨论】:

        • 很抱歉在这里唤醒了一个僵尸,但如果你还有你的代码,我会很感兴趣的!
        • 嗨 bdv。我实际上挖出了我的代码并将其发布到我的原始答案中。我不记得我是怎么做到的,因为我已经有 2 年没有开发过这个应用程序了,但我从 Apple 的原始 PhotoPicker 应用程序开始。 developer.apple.com/library/ios/samplecode/PhotoPicker/…
        【解决方案5】:

        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
        }
        

        【讨论】:

          猜你喜欢
          • 2012-04-13
          • 2018-06-25
          • 2017-10-31
          • 1970-01-01
          • 1970-01-01
          • 2020-09-07
          • 1970-01-01
          • 2012-02-11
          • 1970-01-01
          相关资源
          最近更新 更多