在UIImagePickerController中叠加图像,具体取决于设备方向

时间:2011-07-26 18:07:12

标签: iphone orientation overlay uiimagepickercontroller

我想要在相机视图中显示两个叠加图像,具体取决于设备方向 - 一个用于水平,一个用于垂直方向(overlay_v.png和overlay_h.png)。当设备从纵向旋转到垂直方向时,叠加图像也应该更改。

使用beginGeneratingDeviceOrientationNotifications我只能确定设备方向,但无法管理更改叠加图像。任何帮助/不同的方法将非常感激。

- (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 个答案:

答案 0 :(得分:4)

显示选择器后,您无法更改cameraOverlayView属性。但是,您可以修改视图本身。创建名为overlayView的UIView属性。将两个图像作为子视图添加到此视图,并使用每个图像上的标记属性轻松地再次抓取该视图。隐藏当前方向不应显示的那个。

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

}