2011-12-16 108 views

回答

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

这不会改变相机控制,而是将相机导航条添加到相机窗口的顶部。这可能是也可能不是你想要的,但我不认为这是OP的想法。 – DaGaMs 2012-06-26 07:17:16

+2

伟大的解决方案。请记住检查相机源是否真的可用。例如: if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){ self.picker.sourceType = UIImagePickerControllerSourceTypeCamera; } – EsbenB 2013-05-28 13:26:41

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 
1

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 
} 
相关问题