ios 启用 UIImagePickerController 上的照片库按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8528880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enabling the photo library button on the UIImagePickerController
提问by MBU
Does anyone know how to enable the photo album button on the UIImagePickerController when its in the camera mode? Like how the camera app on on the iphone can toggle between image and video taking and also has the button to view the photo library?
有谁知道如何在相机模式下启用 UIImagePickerController 上的相册按钮?就像 iPhone 上的相机应用程序如何在图像和视频拍摄之间切换,还有查看照片库的按钮?
采纳答案by Kevin Xue
I'm afraid that this can't be done in that easy way (just enable or disable some magic feature). For some simple requirement, you can use cameraOverlayViewand showsCameraControlsto make the implementation.
恐怕这不能以那种简单的方式完成(只需启用或禁用某些神奇功能)。对于一些简单的需求,您可以使用cameraOverlayView和showsCameraControls来实现。
If you want to switch between photo/video mode, i think you can check this demo: http://developer.apple.com/library/ios/#samplecode/AVCam/Introduction/Intro.html
如果你想在照片/视频模式之间切换,我想你可以查看这个演示:http: //developer.apple.com/library/ios/#samplecode/AVCam/Introduction/Intro.html
回答by epsilontik
This can be done via the following lines:
这可以通过以下几行来完成:
- (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;
}
回答by William Entriken
Here is an example app and very simple library that lets you get the Take Photo or Choose from Library just like Facebook does. https://github.com/fulldecent/FDTake
这是一个示例应用程序和非常简单的库,可让您像 Facebook 一样获取“拍照”或“从库中选择”。https://github.com/fulldecent/FDTake
回答by ganime
I have done this tweaking Apple's PhotoPicker example app. I have removed all the camera controls and added my own button. When clicked, UIImagePickerControllerSourceType is set to the UIImagePickerControllerSourceTypePhotoLibrary.
我已经完成了对 Apple 的 PhotoPicker 示例应用程序的调整。我已经删除了所有的相机控件并添加了我自己的按钮。单击时,UIImagePickerControllerSourceType 设置为 UIImagePickerControllerSourceTypePhotoLibrary。
The tricky part for me was "dismissing" (might be technically the wrong word) the Photo Library after the image was picked. I did this by setting the source type back to UIImagePickerControllerSourceTypeCamera. This brings back the camera overlay view.
对我来说棘手的部分是在选择图像后“关闭”(技术上可能是错误的词)照片库。我通过将源类型设置回 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
回答by ZekeMidas
Swift2 version of @epsilontik code:
@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
}