如何直接在主视图控制器显示相机隐藏所有默认按钮,并采取我们的自定义按钮,当我点击按钮,我需要在UIImageView中的照片?如何在完成启动后直接打开相机?
-3
A
回答
0
尝试为SWIFT 3 ....
override func viewDidAppear(animated: Bool)
{
if UIImagePickerController.isCameraDeviceAvailable(UIImagePickerControllerCameraDevice.Front)
{
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
presentViewController(imagePicker, animated: true, completion: nil)
}
}
0
添加UIImagePickerControllerDelegate代表在.h文件中,并在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];
}
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];
}
1
要隐藏你需要设置的UIImagePickerController
showsCameraControls= NO
财产所有默认的按钮。
,并添加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];
}
添加您的委托方法来获得委托方法图像数据
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
//Access the uncropped image from info dictionary
[picker dismissViewControllerAnimated:YES completion:^{
UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
if(image)
}];
}
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];
}
0
这是swift 3用于捕获摄像头图像的代码,当您单击captureCamera按钮时。
@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)
}
不要忘了这个picker.delegate = self
添加到您的viewDidLoad()函数。
相关问题
- 1. 如何在全景/光圈模式下直接打开相机?
- 2. 打完电话后直接重新打开活动
- 3. 如何在我的活动中启动系统相机后以正常操作打开系统相机?
- 4. 安装完成后启动应用程序,并打开UAC
- 5. 如何直接从网页启动相机android
- 6. 如何完成由相机
- 7. 如何在打印机作业完成后停止打印java
- 8. 如何打开photosphere相机?
- 9. 如何在构建器完成后自动重新启动buildslave主机?
- 10. 如何在应用程序启动完成后生成启动图像
- 11. JProgressBar不启动直到Try-catch完成
- 12. 如何让Spark在完成后自动重新启动作业?
- 13. 在应用程序启动时打开相机视图(iOS6)
- 14. 保持FTP连接打开直到完成任务
- 15. 如何在尺寸内打开相机?
- 16. 如何在android webview中打开相机?
- 17. 如何直接从Atom打开链接
- 18. AsyncTask在函数完成后启动
- 19. 启动和活动完成后活动
- 20. 自动打开完成窗口在Eclipse
- 21. 在UIView开始动画后完成动作完成
- 22. 在TransactionScope完成后,底层连接无法打开
- 23. 直接从代码打开相机设置
- 24. 如何在相机闪光灯开启与commonsware相机库
- 25. 如何保持Python服务器连接打开,直到客户端完成它?
- 26. grails jms在完成启动后开始监听
- 27. 如何在windows phone 7启动完成后自动启动应用程序
- 28. 如何在JProgressBar完成后打开新窗口
- 29. 如何在启动任务后直接解开绑定的按钮?
- 30. 完成后再次启动jquery动画
[先看应用程序生命周期过程](http://stackoverflow.com/a/14644625/4003548),然后应用相机代码。 – vaibhav
这是一个旧文档,你可能想引用这个:https://developer.apple.com/reference/uikit/uiviewcontroller – holex