简体   繁体   English

进入UIImagePickerController编辑模式

[英]Entering UIImagePickerController editing mode

I use a UIImagePickerController ( photoPicker ) to take pictures with the camera, and edit those picture ( allowEditing = YES ). 我使用UIImagePickerControllerphotoPicker )用相机拍照,并编辑那些图片( allowEditing = YES )。 The user can switch between Camera and Library with a button in photoPicker.view , but I want to remove this button when photoPicker is in editing mode. 用户可以使用photoPicker.view的按钮在相机和库之间切换,但我想在photoPicker处于编辑模式时删除此按钮。

In photoLibrarySourceType the picker uses a push to show editing mode, so it works with this method: photoLibrarySourceType ,选择器使用推photoLibrarySourceType显示编辑模式,因此它适用于此方法:

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (navigationController == photoPicker) {
        NSLog(@"navigation ");
        if ([navigationController.viewControllers indexOfObject:viewController] == 1)
        {
            // If second UIViewController, set your overlay.
            NSLog(@"editing");
        }
    }
}

But in cameraSourceType , there is no navigation between camera and editing mode. 但是在cameraSourceType ,相机和编辑模式之间没有导航。

I came up with a little hack and it works for me. 我想出了一个小黑客,它对我有用。 Am still a little new so please optimize at will as i couldn't find another way to do it. 我仍然有点新,所以请随意优化,因为我找不到另一种方法来做到这一点。

The basic idea is to create an invisible button over the default camera button that performs a method that calls [uiimagepickercontroller takePicture] 基本思路是在默认的相机按钮上创建一个不可见的按钮,该按钮执行一个调用[uiimagepickercontroller takePicture]的方法

here is my sample code 这是我的示例代码

@implementation myViewController
{
    UIImagePickerController *myImagePickerController; 
    UIButton * fakeCameraButton;


}

- (IBAction)showImagePicker:(id)sender{

    // accessible camera validations implied
    myImagePickerController = [[UIImagePickerController alloc]init];
    myImagePickerController.sourceType=UIImagePickerControllerSourceTypeCamera;       
    myImagePickerController.showsCameraControls = YES;
    myImagePickerController.delegate=self;
    myImagePickerController.allowsEditing=YES; 

    CGFloat myViewHeight = 100; // you play around with the positions to get your desired frame size. This worked for me
    UIView * myView = [[UIView alloc] initWithFrame:CGRectMake(0, myImagePickerController.view.frame.size.height - myViewheight, picker.view.frame.size.width, myViewheight)];

   fakeCameraButton = [[UIButton alloc] initWithFrame:CGRectMake(myView.frame.size.width / 2 - 30, myView.frame.size.height/2-15, 60, 60)];// position it over the default camera button
   [fakeCameraButton addTarget:self action:@selector(cameraButtonPressed) forControlEvents:UIControlEventTouchUpInside];
   [fakeCameraButton setTitle:@"" forState:UIControlStateNormal];

   [myView addSubview:fakeCameraButton];
   myImagePickerController.cameraOverlayView = myView;
   [self presentViewController:myImagePickerController animated:YES completion:nil];


}

-(void)cameraButtonPressed{

    // Make sure you check if camera is ready before this method. 
   [myImagePickerController takePicture];

    // you are in editing/preview mode here if allowsEditing = YES

    // you can remove all your custom overlay views by
    myImagePickerController.cameraOverlay = nil



} 

Like i said before just make sure you check if camera is ready. 就像我之前说过的那样,请确保检查相机是否准备就绪。 You can find answers here on stackoverflow on how to do it. 您可以在stackoverflow上找到有关如何操作的答案。

This code may suck so editing or alternative solutions are welcomed. 此代码可能很糟糕,因此欢迎编辑或替代解决方案。

I need dev buddy to help me learn more about ios dev and ask questions. 我需要开发伙伴来帮助我了解更多有关ios dev的信息并提出问题。 anyone interested? 有兴趣吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM