如何在隐藏所有默认按钮的情况下直接显示主视图控制器中的摄像头并选择一个我们的自定义按钮,当我点击UIImageview中需要照片的按钮时?
答案 0 :(得分:1)
要隐藏所有默认按钮,您需要设置showsCameraControls= NO
的{{1}}属性。
在UIImagePickerController
UIImagePickerController
现在在您的自定义按钮操作方法中添加代码以拍摄照片
UIImagePickerController *imgPicker;
-(void)openCustomCamera{
imgPicker = [[UIImagePickerController alloc] init];
[imgPicker setDelegate:self];
[imgPicker setSourceType:UIImagePickerControllerSourceTypeCamera];
imgPicker.showsCameraControls = NO;
//Custom button
UIButton *customBtn = [[UIButton alloc] initWithFrame:CGRectZero];
[customBtn addTarget:self action:@selector(customButtonAction:) forControlEvents:UIControlEventTouchUpInside];
//Add your custom button to imagepickerview
[imgPicker.view addSubView:customBtn];
// Show image picker
[self presentViewController:imagePicker animated:YES completion:^{}];
}
添加委托方法以获取委托方法
中的图像数据-(void)customButtonAction:(id)sender{
[imgPicker takePicture];
}
答案 1 :(得分:1)
Add this code in app delegate and call openCamera in applicationDidBecomeActive
-(void)openCamera
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.allowsEditing = YES;
[self.window.rootViewController presentViewController:imagePicker animated:true completion:nil];
}else{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Camera not available"
message:@"No Camera available on your device."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alert show];
alert = nil;
}
}
Add Delegates
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self.window.rootViewController dismissViewControllerAnimated:true completion:nil];
}
答案 2 :(得分:0)
尝试快速3 ....
override func viewDidAppear(animated: Bool)
{
if UIImagePickerController.isCameraDeviceAvailable( UIImagePickerControllerCameraDevice.Front)
{
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
presentViewController(imagePicker, animated: true, completion: nil)
}
}
答案 3 :(得分:0)
在.h文件中添加UIImagePickerControllerDelegate委托,并在didfinish启动时实现。不要忘记实现委托。
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = NO;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
else
{
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Device has no camera!"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[myAlertView show];
}
代表:
#pragma mark - UIImagePickerController Deleagte
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:NULL];
UIImage *image =info[UIImagePickerControllerOriginalImage];
// NOW set the image on you imageview like
self.myImageview.image=image;
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
答案 4 :(得分:0)
按照以下步骤操作,希望它对您有用。
1)实施UIImagePickerControllerDelegate
2)当您的应用程序生效时调用
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSLog(@"appdelegate applicationDidBecomeActive");
[self actionLaunchAppCamera];
}
3)打开相机的方法
-(void)actionLaunchAppCamera
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.allowsEditing = YES;
[self.window.rootViewController presentModalViewController:imagePicker animated:YES];
}else{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Camera Unavailable"
message:@"Unable to find a camera on your device."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alert show];
alert = nil;
}
}
4)定义委托方法如下。
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self.window.rootViewController dismissModalViewControllerAnimated:YES];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self.window.rootViewController dismissModalViewControllerAnimated:YES];
}
答案 5 :(得分:0)
单击captureCamera按钮时,这是从相机捕获图像的 swift 3 代码。
authorization
不要忘记将此@IBAction func captureCamera(sender: UIBarButtonItem) {
if UIImagePickerController.availableCaptureModesForCameraDevice(.Rear) != nil {
picker.allowsEditing = false
picker.sourceType = UIImagePickerControllerSourceType.Camera
picker.cameraCaptureMode = .Photo
presentViewController(picker, animated: true, completion: nil)
} else {
noCamera()
}
}
func imagePickerController(
picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [String : AnyObject])
{
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage //2
myImageView.image = chosenImage //4
dismissViewControllerAnimated(true, completion: nil) //5
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
添加到viewDidLoad()函数中。