iOS:使用takePicture方法时发生崩溃

时间:2014-01-08 10:53:02

标签: ios iphone ios7 uiimagepickercontroller

我正在使用自定义UIImagePickerController并且我在同一个控制器中也需要UINavigationBar,因此我尝试利用导航栏本身的空间,而不是显示默认的相机捕获按钮,在按钮动作我试图调用拍照方法,但它崩溃了。什么是正确的程序。

    // prepare imagePicker view
    imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
    imagePicker.showsCameraControls = NO;
    isFront = YES;
    cameraType = @"front";

    UIBarButtonItem *takePictureButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:imagePicker.view action:@selector(captureImage:)];

    UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(flip:)];

self.navigationItem.rightBarButtonItems = [[NSArray alloc]initWithObjects:takePictureButton,cancelButton, nil];

-(void)captureImage:(id)sender
{
    [imagePicker takePicture];
}

- (void) flip: (id) sender
{
    if(isFront)
    {
        cameraType = @"front";
        isFront = NO;
        imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
    }
    else
    {
        isFront = YES;
        cameraType = @"rear";
        imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceRear;

    }
}

1 个答案:

答案 0 :(得分:3)

您正在尝试从imagePicker.view调用一个未在此处声明的方法。

替换

UIBarButtonItem *takePictureButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:imagePicker.view action:@selector(captureImage:)];

UIBarButtonItem *takePictureButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(captureImage:)];