8

我有按钮,当我单击时会显示相机(完美运行)

- (IBAction)getPhoto:(id)sender
{
    NSLog( @"Button clicked!" );
    imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        [imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
    }

    else {  // IF the device doesn't have a camera, so use the photos album
        [imagePicker setSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
    }

    [self addSubview:imagePicker.view];
}

之后我可以拍摄图像等,但是当我单击“使用”时:这里什么都没有发生是我的方法:图像选择器不会关闭其自身:应用程序根本什么都不做,我正在尝试拍摄我刚刚拍摄的图像将其显示在屏幕上的 CGRect 中。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissViewControllerAnimated:YES completion:NULL];

    UIImageView *patientImage = [[UIImageView alloc] init];
    patientImage.frame = CGRectMake(625, 25, 83, 103);

    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    self.patientImage.image = chosenImage;
}

即使我在没有拍摄图像的情况下点击取消按钮,imagePicker(相机界面)也不会自行关闭

任何帮助、建议或指导将不胜感激。

4

6 回答 6

5

你不应该在那里做“ addSubview”的事情,而是做:

[self presentViewController:imagePicker animated:YES completion:nil];
于 2013-06-21T09:57:38.980 回答
5

与其将选择器添加为子视图,不如将选择器添加到UIPopoverController.

然后,您可以UIView使用presentPopoverFromRect:inView:permittedArrowDirections:animated:popover 方法从子类中显示它,如下所示:

self.popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
[self.popover presentPopoverFromRect:sender.frame inView:self permittedArrowDirections: UIPopoverArrowDirectionAny animated:YES];

然后,您可以使用委托方法来关闭弹出框:

[self.popover dismissPopoverAnimatedYES];

官方文档要求:

该表表明,在 iPad 上,如果指定源类型UIImagePickerControllerSourceTypePhotoLibraryUIImagePickerControllerSourceTypeSavedPhotosAlbum,则必须使用弹出框控制器呈现图像选择器,如 UIPopoverController 类参考中的“呈现和关闭弹出框”中所述。如果您尝试以模态方式(全屏)显示图像选择器以在保存的图片和电影中进行选择,系统会引发异常。

在 iPad 上,如果您指定源类型 UIImagePickerControllerSourceTypeCamera,您可以以模态方式(全屏)或使用弹出框呈现图像选择器。但是,Apple 建议您仅全屏显示相机界面。

于 2013-06-28T01:28:06.697 回答
4

getPhoto:方法在子类中吗UIView

如果是这样,那么最好:

  1. getPhoto:代码完全移出UIView子类,并将其放在视图的父视图控制器中。

  2. 使您UIButton的 UIView 的属性:@property (nonatomic, strong) UIButton *myButton;

  3. 在您的视图控制器中,按如下方式设置按钮的目标和操作:

    [myView.myButton addTarget:self action:@selector(getPhoto:) forControlEvents:UIControlEventTouchUpInside];
    
  4. 最后,在您的getPhoto:方法中,调用[self presentViewController:imagePicker animated:YES completion:NULL];

  5. 您的- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info代码也应该进入视图控制器,更改self.patientImage.image = chosenImage;myView.patientImage.image = chosenImage;

于 2013-06-23T23:10:41.550 回答
1

尝试:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker.view removeFromSuperview];

    UIImageView *patientImage = [[UIImageView alloc] init];
    patientImage.frame = CGRectMake(625, 25, 83, 103);

    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    self.patientImage.image = chosenImage;
}
于 2013-06-28T01:09:28.517 回答
1

不知道为什么取消按钮不起作用。但是进行此更改它将选择您正在单击的图像。

如果您想要取消事件,请通过创建自定义UIBarButton添加它并添加它。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

UIImageView *patientImage = [[UIImageView alloc] init];
patientImage.frame = CGRectMake(625, 25, 83, 103);

UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.patientImage.image = chosenImage;

[imagePicker.view removeFromSuperview];

}
于 2013-06-24T07:50:10.020 回答
-1

删除[self.addSubView:imagePicker.view];and 代替

[self presentViewController:imagePicker animated:YES completion:NULL];

picker尽管picker未定义,但您正在解雇变量。而是放:

[self dismissModalViewControllerAnimated:YES completion:NULL]; 并确保它在你的方法结束。

于 2013-06-21T09:59:20.570 回答