UIImagePickerController overlayView和showsCameraControls问题

时间:2012-07-08 18:54:31

标签: iphone ios5 camera uiimagepickercontroller camera-overlay

我正在尝试在UIImagePickerController上使用自定义叠加层,并且还可以移动和缩放生成的图像。问题是当我设置showsCameraControls=YES时,叠加层不会显示,但我可以使用移动和缩放功能。但是,当showsCameraControls=NO时,会出现叠加层,但是当我拍摄照片时,我甚至无法裁剪图像。从xib加载的实际视图只是一个带有两个按钮的UIToolbar,其中一个带有图片

我有一个控制器(OverlayViewController),它从xib文件中加载叠加层。此控制器还有一个UIImagePickerController,因此它会将UIImagePickerController的叠加层设置为OverlayController的视图。第一种方法在不同的控制器中调用,将其全部设置并显示出来。总之,我需要能够看到overlayView并使用它的按钮拍摄照片,然后我可以移动并缩放

-(void) openCameraForImage
{

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        self.overlayViewController =
        [[OverlayViewController alloc] initWithNibName:@"OverlayViewController" bundle:nil];

        // as a delegate we will be notified when pictures are taken and when to dismiss the image picker
        self.overlayViewController.delegate = self;

        [self.overlayViewController setupImagePicker:UIImagePickerControllerSourceTypeCamera];
        [self presentModalViewController:self.overlayViewController.imagePickerController animated:YES];
    }
    else {
        [Common ShowAlert:@"Alert" andMessage:@"There is no camera on this device"];
    }

}

此方法位于OverlayViewController,负责设置叠加层。

- (void)setupImagePicker:(UIImagePickerControllerSourceType)sourceType
{
self.imagePickerController.sourceType = sourceType;
[self.imagePickerController setAllowsEditing:YES];

if (sourceType == UIImagePickerControllerSourceTypeCamera)
{
    // user wants to use the camera interface
    //
    self.imagePickerController.showsCameraControls = NO; //This line allows overlay to show but move and scale doesn't work
    self.imagePickerController.allowsEditing = YES;
    //[self.view setUserInteractionEnabled:NO];

    if ([[self.imagePickerController.cameraOverlayView subviews] count] == 0)
    {
        // setup our custom overlay view for the camera
        //
        // ensure that our custom view's frame fits within the parent frame
        CGRect overlayViewFrame = self.imagePickerController.cameraOverlayView.frame;
        CGRect newFrame = CGRectMake(0.0,
                                     CGRectGetHeight(overlayViewFrame) -
                                     self.view.frame.size.height - 10.0,
                                     CGRectGetWidth(overlayViewFrame),
                                     self.view.frame.size.height + 10.0);
        self.view.frame = newFrame;
        self.imagePickerController.cameraOverlayView = self.view;
        }
    }
}

OverlayViewConroller中的此方法拍摄照片

- (IBAction)takePhoto:(id)sender
{
    [self.imagePickerController takePicture];
}

1 个答案:

答案 0 :(得分:0)

我认为你可以将你的图像选择器代码放在openCameraForImage

在你的OverlayViewController中,只需使用它来纯粹显示相机的叠加效果。

@implementation RiskCameraOverlayViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self.view setBackgroundColor:[UIColor blackColor]];

    opUpImage = [UIImage imageNamed:@"camera_overlay_op_up"];
    opDownImage = [UIImage imageNamed:@"camera_overlay_op_down"];

    opUpFrameBegin = CGRectMake(0, -50, 320, 358);
    opUpImageView = [[UIImageView alloc] initWithFrame:opUpFrameBegin];
    [opUpImageView setImage:opUpImage];

    opDownFrameBegin = CGRectMake(0, 265, 320, 314);
    opDownImageView = [[UIImageView alloc] initWithFrame:opDownFrameBegin];
    [opDownImageView setImage:opDownImage];

    [self.view addSubview:opUpImageView];
    [self.view addSubview:opDownImageView];

}

您可能会遇到“使用照片”或“重新拍摄”无效的问题,请执行

[self.view removeFromSuperview];