1

我正在创建一个应用程序,它从 iphone 相机捕获图像并将其显示在创建的应用程序中我阅读了 ios 开发人员库中的一些文章并将此代码编写在 .m 文件中

- (BOOL) startCameraControllerFromViewController: (UIViewController*) controller
           usingDelegate: (id <UIImagePickerControllerDelegate,
                               UINavigationControllerDelegate>) delegate {

if (([UIImagePickerController isSourceTypeAvailable:
             UIImagePickerControllerSourceTypeCamera] == NO)
        || (delegate == nil)
        || (controller == nil))
    return NO;


UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;

// Displays a control that allows the user to choose picture or
// movie capture, if both are available:
cameraUI.mediaTypes =
    [UIImagePickerController availableMediaTypesForSourceType:
        UIImagePickerControllerSourceTypeCamera];

// Hides the controls for moving & scaling pictures, or for
// trimming movies. To instead show the controls, use YES.
cameraUI.allowsEditing = NO;

cameraUI.delegate = delegate;

[controller presentModalViewController: cameraUI animated: YES];
return YES;
}

还创建了一个与此代码链接的按钮,它调用此函数但控件不传递给上述函数

- (IBAction) showCameraUI {
[self startCameraControllerFromViewController: self
                                usingDelegate: self];
}

而且我也不知道这段代码是否有效

有人有更好的主意吗。谢谢,阿伦。

4

2 回答 2

2

首先将委托分配给它的源文件

然后,当您按下按钮时,实现此方法在您的应用程序中打开相机控制器

首先为 iPad 创建 PopOverController 因为我们不能直接打开 ImagePicker Controller

编辑:添加 UIPopOverController

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

     if (self.popoverController != nil) {
        [self.popoverController dismissPopoverAnimated:YES];
        self.popoverController=nil;
     }

     self.popoverController = [[UIPopoverController alloc] initWithContentViewController:picker];
      CGRect popoverRect = [self.view convertRect:[yourBtn frame]
                                   fromView:[yourBtn superview]];

     popoverRect.size.width = MIN(popoverRect.size.width, 100) ;
     popoverRect.origin.x = popoverRect.origin.x;

     [self.popoverController
     presentPopoverFromRect:popoverRect
     inView:self.view
      permittedArrowDirections:UIPopoverArrowDirectionAny
     animated:YES];

    }

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

- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissModalViewControllerAnimated:YES];
    UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
    UIImage *yourImageView = image;
    if (popoverController != nil) {
     [popoverController dismissPopoverAnimated:YES];
     self.popoverController=nil;
     }
}

如果用户取消控制器,请执行此方法

- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker
{
    [picker dismissModalViewControllerAnimated:YES];
     if (popoverController != nil) {
     [popoverController dismissPopoverAnimated:YES];
     self.popoverController=nil;
     }
}

- (void)pickerDone:(id)sender
{
     if (popoverController != nil) {
     [popoverController dismissPopoverAnimated:YES];
     self.popoverController=nil;
     }
}
于 2013-02-06T12:54:46.530 回答
0

您可以使用委托: UIImagePickerControllerDelegate

使用如下:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)mediaAndInfo

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
于 2013-02-06T12:53:26.650 回答