【问题标题】:Overlay image in UIImagePickerController depending on device orientationUIImagePickerController 中的覆盖图像取决于设备方向
【发布时间】:2011-07-26 18:07:11
【问题描述】:

我有两个叠加图像,我想根据设备方向在相机视图中显示 - 一个用于水平方向,一个用于垂直方向(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");
    }

}

【问题讨论】:

  • 您想知道在 didOrientation 方法中如何将第一个图像视图更改为第二个图像视图?

标签: iphone orientation overlay uiimagepickercontroller


【解决方案1】:

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

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

}

【讨论】:

    猜你喜欢
    • 2011-04-13
    • 2015-12-05
    • 2014-04-13
    • 2018-11-29
    • 2013-09-30
    • 2018-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多