iOS:UIImagePickerController - 使用工具栏属性?

Posted

技术标签:

【中文标题】iOS:UIImagePickerController - 使用工具栏属性?【英文标题】:iOS: UIImagePickerController - using toolbar property? 【发布时间】:2012-07-24 18:41:54 【问题描述】:

鉴于以下代码,按钮不会显示在工具栏上。有什么不对?

UIImagePickerController* cameraPickerController = [[UIImagePickerController alloc] init];
cameraPickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraPickerController.delegate =self;
cameraPickerController.showsCameraControls = NO;
cameraPickerController.toolbarHidden = NO;

UIBarButtonItem *cancelBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target: self action: @selector(cancel)];
UIBarButtonItem *flexibleBarSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
flexibleBarSpace.width = 1000;
UIBarButtonItem *cameraBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:cameraPickerController action:@selector(takePicture)];
UIBarButtonItem *faceBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Show facemask" style:UIBarButtonItemStyleBordered target: self action: @selector(showFacemask)];
cameraPickerController.toolbar.items = [NSArray arrayWithObjects:cancelBarButtonItem,flexibleBarSpace,cameraBarButtonItem,flexibleBarSpace,faceBarButtonItem,nil];

[self presentModalViewController:cameraPickerController animated:YES];

理论上,那些 cancelBarButtons 和其他应该显示在工具栏上。但是,在这种情况下不会。请指教。

【问题讨论】:

【参考方案1】:

您不能将项目添加到工具栏,因为它还没有被UIImagePickerController 呈现(我想。)试试这个:

UIImagePickerController* cameraPickerController = [[UIImagePickerController alloc] init];
cameraPickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraPickerController.delegate =self;
cameraPickerController.showsCameraControls = NO;

UIToolbar *theToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 436, 320, 44)];

UIBarButtonItem *cancelBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target: self action: @selector(cancel)];
UIBarButtonItem *flexibleBarSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
flexibleBarSpace.width = 1000;
UIBarButtonItem *cameraBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:cameraPickerController action:@selector(takePicture)];
UIBarButtonItem *faceBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Show facemask" style:UIBarButtonItemStyleBordered target: self action: @selector(showFacemask)];

theToolbar.items = [NSArray arrayWithObjects:cancelBarButtonItem, flexibleBarSpace, cameraBarButtonItem, faceBarButtonItem, nil];

[cameraPickerController setCameraOverlayView:theToolbar];

[self presentModalViewController:cameraPickerController animated:YES];

基本上我只是创建了一个新工具栏并将其设置为cameraOverlayView

干杯!

【讨论】:

我也和你有同样的想法。我这样做了,令我惊讶的是,它显示了一个空栏。我知道代码有效,因为在其他地方,我使用类似的代码并且工具栏有效。我相信这里隐藏着一些东西可以阻止它。你自己试过吗?它对你有用吗? 是的,我尝试了这个确切的代码,它对我有用。您在上面发布的代码只是向我显示了一个空白工具栏。 我确实摆脱了 toolbarHidden=no,也许是这样? 我刚刚又试了一次,它确实有效。我不确定我之前做了什么。谢谢。【参考方案2】:

为了那些搜索和找到这篇文章的人的利益,迟到的答案。

ios 5及以后的版本中,解决方法是调用presentViewController:animated:completion,在完成块中设置工具栏项。

    UIImagePickerController* cameraPickerController = [[UIImagePickerController alloc] init];
    cameraPickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
    cameraPickerController.delegate =self;
    cameraPickerController.showsCameraControls = NO;
    cameraPickerController.toolbarHidden = NO;

    UIBarButtonItem *cancelBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target: self action: @selector(cancel)];
    UIBarButtonItem *flexibleBarSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    flexibleBarSpace.width = 1000;
    UIBarButtonItem *cameraBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:cameraPickerController action:@selector(takePicture)];
    UIBarButtonItem *faceBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Show facemask" style:UIBarButtonItemStyleBordered target: self action: @selector(showFacemask)];

    //cameraPickerController.toolbar.items = [NSArray arrayWithObjects:cancelBarButtonItem,flexibleBarSpace,cameraBarButtonItem,flexibleBarSpace,faceBarButtonItem,nil];
    NSArray *tbItems = [NSArray arrayWithObjects:cancelBarButtonItem,flexibleBarSpace,cameraBarButtonItem,flexibleBarSpace,faceBarButtonItem,nil];

    // [self presentModalViewController:cameraPickerController animated:YES];
    cameraPickerController.modalTransitionStyle = UIModalPresentationFullScreen;
    [self presentViewController:picker animated:YES completion:^
        cameraPickerController.toolbar.items = tbItems;
    ];

【讨论】:

【参考方案3】:

于是我搜索了类似的问题,发现this

但基本上他的问题是他正在尝试

[self presentModalViewController:foo animated:YES];

修复是

[self.tabBarController presentModalViewController:camera animated:YES];

看看这是否也能解决你的问题。没有看到更多代码很难确定

【讨论】:

抱歉没有解决。我根本没有使用 TabbarController。

以上是关于iOS:UIImagePickerController - 使用工具栏属性?的主要内容,如果未能解决你的问题,请参考以下文章

imagePickerController 选择照片时出错

iPhone - 以编程方式删除状态栏

如何调整自定义 UIButton 的图像大小?

从保存的相机胶卷参考 url 设置 uiimageview

Segue 图像到另一个视图控制器的问题

{python之IO多路复用} IO模型介绍 阻塞IO(blocking IO) 非阻塞IO(non-blocking IO) 多路复用IO(IO multiplexing) 异步IO