1

我正在尝试将相机看到的图像显示到我自己的自定义视图中。我不想为 UIImagePickerController 使用 Apple 自己的覆盖属性:cameraOverlayView。

我不想要叠加层,而是让它只显示在我的特定视图中。我能够做到这一点的唯一方法是在我的 UIView 中创建一个子视图,但是这样做不允许我使用我在 UIView 上制作的任何我自己的自定义按钮。到目前为止,我的代码是:

UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];

[imagePickerController setSourceType:UIImagePickerControllerSourceTypeCamera];
[imagePickerController setShowsCameraControls:NO];
[imagePickerController setEditing:NO];
[imagePickerController setNavigationBarHidden:YES];

[self.videoPreviewView addSubview:[imagePickerController view]];
[imagePickerController viewWillAppear:YES];

videoPreviewView 是我的 UIView 的出口!

有什么建议么?谢谢!

4

2 回答 2

2

我自己没有尝试过你的方式,但我也想创建一个自定义的相机视图。这可能不是您想要的,但它是一种替代方法。我与 .xib 文件一起创建了一个自定义 UIView。

在 .m 文件中,您只需将适当的委托方法调用到您实例化 imagePicker 的 ViewController。然后在此 ViewController 中调用适当的方法,例如takePicture.

这样,您可以使用 xib 文件使覆盖看起来像您喜欢的任何方式。您可以实例化图像选择器

    FALKCameraView.h :

        @protocol CameraViewDelegate <NSObject>

        - (void)cameraPressed;

        - (void)buttonPressed;
        //etc


        @end

        @interface FALKCameraView : UIView

        @property (nonatomic, assign) NSObject<CameraViewDelegate> *delegate;

        //Camera Button pressed- notifies CameraViewDelegate
        - (IBAction)cameraPressed:(id)sender;
        //Button pressed - notifies CameraViewDelegate
        -(IBAction)buttonPressed:(id)sender;


        @end

    FALKCameraView.m:

    @implementation FALKCameraView

    @synthesize delegate;

    - (IBAction)cameraPressed:(id)sender{
        [delegate cameraPressed];
    }

    -(IBAction)buttonnPressed:(id)sender{
        [delegate buttonPressed];
    }


    @end


    FALKCameraViewController.h:

    @interface FALKCameraViewController : UIViewController <UIImagePickerControllerDelegate, CameraViewDelegate>

    - (void)cameraPressed;

    - (void)buttonPressed;

    @end


    FALKCameraViewController.m:

    @interface FALKCameraViewController
         @property (strong,nonatomic) UIImagePickerController *imagePicker;
    @end

    @implementation FALKCameraViewController
    @synthesise imagePicker;

    -(void)setUpCamera{
        if ([UIImagePickerController isSourceTypeAvailable:
                 UIImagePickerControllerSourceTypeCamera]){
                imagePicker = [[UIImagePickerController alloc] init];
                imagePicker.delegate = self;
                imagePicker.sourceType =
                UIImagePickerControllerSourceTypeCamera;
                imagePicker.showsCameraControls = NO;
                imagePicker.allowsEditing = NO;


imagePicker.mediaTypes = @[(NSString *) kUTTypeImage];



                NSString *nibName = @"FALKCameraView";

                NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:nibName
                                                              owner:nil
                                                            options:nil];
                if([nibs count]){
                    id nib = [nibs objectAtIndex:0];
                    if([nib isKindOfClass:[FALKCameraView class]]){
                        FALKCameraView *view = (FALKCameraView*)nib;
                        view.delegate = self;
                        imagePicker.cameraOverlayView = view;
                    }
                }

                [self presentViewController:imagePicker
                                   animated:NO completion:nil];
            }
    }



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

    @end
于 2013-05-10T09:08:45.480 回答
1

希望它会起作用,我只是添加了一个视图,您可以将任何自定义对象添加到选择器的覆盖视图

   if ([UIImagePickerController isSourceTypeAvailable:
     UIImagePickerControllerSourceTypeCamera]){
    imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType =
    UIImagePickerControllerSourceTypeCamera;
    imagePicker.showsCameraControls = NO;
    imagePicker.allowsEditing = NO;

    NSString *nibName = @"CameraView";

    NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:nibName
                                                  owner:nil
                                                options:nil];
    if([nibs count]){
        id nib = [nibs objectAtIndex:0];
        if([nib isKindOfClass:[CameraView class]]){
            CameraView *view = (CameraView*)nib;
            view.delegate = self;
            imagePicker.cameraOverlayView = view;
        }
    }

    [self presentViewController:imagePicker
                       animated:NO completion:nil];
}
于 2013-05-10T08:59:43.197 回答