2016-10-14 68 views
-3

如何直接在主视图控制器显示相机隐藏所有默认按钮,并采取我们的自定义按钮,当我点击按钮,我需要在UIImageView中的照片?如何在完成启动后直接打开相机?

+0

[先看应用程序生命周期过程](http://stackoverflow.com/a/14644625/4003548),然后应用相机代码。 – vaibhav

+0

这是一个旧文档,你可能想引用这个:https://developer.apple.com/reference/uikit/uiviewcontroller – holex

回答

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

要隐藏你需要设置的UIImagePickerControllershowsCameraControls= 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

_“在代码中添加此代码并调用applicationDidBecomeActive中的openCamera”_。不要吧__。 – holex

+0

它不适合你吗?它为我工作。 – User511

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()函数。

相关问题