4

I'm trying to use a custom overlay on the UIImagePickerController and also be able to move and scale the resulting image. The problem is when I set showsCameraControls=YES, the overlay won't show but I can use the the move and scale functionality. However, when showsCameraControls=NO, the overlay appears, but when I take the picture, I don't even get the option to crop the image. The actual view loaded from the xib is just a UIToolbar with two buttons, one of which takes the picture

I have a controller (OverlayViewController) which loads the overlay from a xib file as it's view. This controller also has a UIImagePickerController so it sets the overlay of the UIImagePickerController to the view of the OverlayController. This first method is called in a different controller to set it all up and display it. In summary, I need to be able see the overlayView and use it's button to take the picture, which I can then move and scale

-(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"];
    }

}

This method is in OverlayViewController and does the work of setting up the Overlay.

- (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;
        }
    }
}

This method in OverlayViewConroller takes the picture

- (IBAction)takePhoto:(id)sender
{
    [self.imagePickerController takePicture];
}
rog
  • 5,351
  • 5
  • 33
  • 40
NickDK
  • 999
  • 10
  • 24

1 Answers1

0

I think you can put your image picker code in openCameraForImage

In your OverlayViewController, just use it to purely show the Overlay for your camera.

@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];

}

You might reach an issue where "Use photo" or "Retake" is not working, do a

[self.view removeFromSuperview];
amdstorm
  • 66
  • 6