启用UIImagePickerController上的照片库按钮
有谁知道如何启用相机模式下的UIImagePickerController上的相册按钮?就像iPhone上的相机应用程序可以在图像和视频拍摄之间切换,并且还具有查看照片库的按钮?启用UIImagePickerController上的照片库按钮
恐怕不能以这种简单的方式完成(只是启用或禁用某些魔术功能)。对于一些简单的要求,您可以使用cameraOverlayView和showsCameraControls来实现。
如果要照片/视频模式之间进行切换,我想你可以检查此演示: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;
}
这不会改变相机控制,而是将相机导航条添加到相机窗口的顶部。这可能是也可能不是你想要的,但我不认为这是OP的想法。 – DaGaMs 2012-06-26 07:17:16
伟大的解决方案。请记住检查相机源是否真的可用。例如: if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){ self.picker.sourceType = UIImagePickerControllerSourceTypeCamera; } – EsbenB 2013-05-28 13:26:41
我已经做了这个调整苹果的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
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
}
啊确定。谢谢!! – MBU 2011-12-16 19:05:54