3
votes

I have two overlay images that I want show in camera view depending on the device orientation - one for the horizontal and one for vertical orientation (overlay_v.png and overlay_h.png). When device is rotated from portrait to vertical orientation, overlay image should change as well.

With beginGeneratingDeviceOrientationNotifications I am able only to determine the device orientation but cannot manage to change the overlay images. Any help / different approach would be very appreciated.

- (IBAction) getPhoto:(id) sender {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;

    if((UIButton *) sender == choosePhotoBtn) {
        sourceCamera = false;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    } else {
        sourceCamera = true;
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;

        //overlay image (cannot dynamically switch from vertical to horizontal in here)
        UIImageView *anImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"overlay_v.png"]];            
        anImageView.frame = CGRectMake(0, 0, anImageView.image.size.width, anImageView.image.size.height);
        picker.cameraOverlayView = anImageView;

        //device orientation check   
        [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didOrientation:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];

        [anImageView release];        
    }
    [self presentModalViewController:picker animated:YES];

}  

- (void) didOrientation: (id)object {
    UIInterfaceOrientation interfaceOrientation = [[object object] orientation];

    if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
        NSLog(@"portrait");
    } else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft ){
        NSLog(@"landscape");
    }

}
1
You want to know how you change the first image view by the second one in didOrientation method?reinaldoluckman

1 Answers

4
votes

You can't change the cameraOverlayView property once the picker is displayed. However, you can modify the view itself. Create a UIView property named overlayView. Add both images as subViews to this view and use the tag property on each image to easily grab that view again. Hide the one that shouldn't be shown for the current orientation.

- (void) didOrientation: (id)object {
   UIInterfaceOrientation interfaceOrientation = [[object object] orientation];
   UIImageView *pImageView = [self.overlayView viewWithTag:10];
   UIImageView *lImageView = [self.overlayView viewWithTag:20];

   if (interfaceOrientation == UIInterfaceOrientationPortrait || 
       interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
      NSLog(@"portrait");
      pImageView.hidden = NO;
      lImageView.hidden = YES;
   } else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight || 
      interfaceOrientation == UIInterfaceOrientationLandscapeLeft ){
      NSLog(@"landscape");
      pImageView.hidden = YES;
      lImageView.hidden = NO;
   }

}