根据设备方向在UIImagePickerController中叠加图像

3
我有两个重叠的图像,我希望根据设备方向在相机视图中显示其中一个 - 一个用于纵向方向,一个用于横向方向(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方法中将第一个图像视图更改为第二个图像视图吗? - reinaldoluckman
1个回答

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

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接