-2

已经创建了一个从相机捕获图像的应用程序。这是我的代码

-(IBAction) showCameraUI {
    BOOL hasCamera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
    UIImagePickerController* picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = hasCamera ? UIImagePickerControllerSourceTypeCamera :    UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentModalViewController:picker animated:YES];
}

并实现了这个委托方法来获取捕获的图像

- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissModalViewControllerAnimated:YES];
    UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
    UIImage *yourImageView = image;
}

如果用户取消控制器,则实现此方法

- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker
{
    [picker dismissModalViewControllerAnimated:YES];
}

但它显示了这个例外。有谁知道为什么在执行最后一行函数 showCameraUI 后会显示这样的异常。

UIStatusBarStyleBlackTranslucent 在此设备上不可用。2013-02-07 10:06:06.976 CaptureImage [460:c07] *由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“在 iPad 上,UIImagePickerController 必须通过 UIPopoverController 呈现”

4

2 回答 2

1
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
self.imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
                [self presentModalViewController:self.imgPicker animated:YES];
 }

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
    [[picker parentViewController] dismissModalViewControllerAnimated:YES];
    [self.imgPicker dismissModalViewControllerAnimated:TRUE]; 

   [self performSelectorInBackground:@selector(DoImageConversionInBackGround:) withObject:img ];
    }    
}
于 2013-02-07T07:24:46.847 回答
1

请试试这个

// 从相册中选择一张图片

- (IBAction)imageFromAlbum:(id)sender
{
    imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    [self presentViewController:imagePicker animated:YES completion:nil];
}

// 用相机拍照

- (IBAction)imageFromCamera:(id)sender
{
    imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
    [self presentViewController:imagePicker animated:YES completion:nil];
}


//  Dismiss the image picker on selection and use the resulting image in our ImageView


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(    UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    [imagePicker dismissModalViewControllerAnimated:YES];
    [imageField setImage:image];
}

// 在取​​消时,只关闭选择器控制器

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [imagePicker dismissModalViewControllerAnimated:YES];
}
于 2013-02-07T10:29:53.077 回答