首页 文章

在不支持旋转的iOS应用程序中旋转UIImageView

提问于
浏览
0

我有一个硬编码的应用程序,只支持1个方向,肖像 . 纵向是通过项目设置设置的 .

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{   
      return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

在此视图控制器中,有几个视图,按钮,标签和其他标准iOS控件 . 对于此视图控制器中的所有控件,所需的行为是当用户旋转iOS设备时,应用程序中的任何内容都不会旋转 .

However ,在所有上述视图之下,充当"Background"是嵌入在UIScrollView中的UIImageView . 这是最底层的观点 . 用户可以随时通过启动照片选择器的按钮更改此背景 . UIImageView嵌入在UIScrollView中,因为所需的行为是允许捏合缩放背景图像 .

虽然应用程序不支持旋转,但我希望ImageView的内容在加载时始终正确定向 .

For Example - 请记住,唯一支持的方向是UIInterfaceOrientationPortrait,用户启动应用程序,将iOS设备旋转到LandScape位置 . 在纵向用户选择新的背景图像 . 预期的行为将是基于当前方向选择的图像被定向和方面正确 .

基本上,我正在寻找那个背景图像,以便在被选中时始终具有朝向地球的“重力” . 如果在用户选择新图像然后他们旋转iOS设备后,背景图像不会旋转 . 只有在最初选择图像时才能正确定位 .

我可以使用CGAffineTransformMakeRotation旋转图像,但是通过夹点放大显示出不良行为,因为图像内的像素本身并没有真正改变方向 .

我想我可能需要在用户选择图像时实际创建一个新图像,这是......

UIImage * RightLandscapeImage = [[UIImage alloc] initWithCGImage: userSelectedImage.CGImage
                                                         scale: 1.0
                                                   orientation: UIImageOrientationRight];

我没有看到任何简单的解决方案 . 可能我忽视了一些简单的事情?

FOLLOW UP 这是似乎需要的代码 . 考虑到当前的方向和启动方向 .

UIImage * newImage;
if( startUpOrientation == UIInterfaceOrientationLandscapeRight )
{
    switch (currentOrientation) {
        case UIInterfaceOrientationLandscapeLeft:
          newImage = [[UIImage alloc] initWithCGImage: bgImage.CGImage
                                                                 scale: 1.0
                                                           orientation: UIImageOrientationDownMirrored];

             [self.backgroundImageView setImage:newImage];
            break;
        case UIInterfaceOrientationLandscapeRight:
             [self.backgroundImageView setImage:bgImage];
            break;
        case UIInterfaceOrientationPortraitUpsideDown:
            newImage = [[UIImage alloc] initWithCGImage: bgImage.CGImage
                                                  scale: 1.0
                                            orientation: UIImageOrientationRight];

            [self.backgroundImageView setImage:newImage];

            break;
        default:
            newImage = [[UIImage alloc] initWithCGImage: bgImage.CGImage
                                                  scale: 1.0
                                            orientation: UIImageOrientationLeft];
            [self.backgroundImageView setImage:newImage];
            break;
    }
 }

1 回答

  • 1

    您可以将变换应用于要在willRotateToInterfaceOrientation中旋转的任何视图

    你可以试试这段代码:

    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
        {
            switch (toInterfaceOrientation) {
                case UIInterfaceOrientationLandscapeLeft:
                    yourview.transform = CGAffineTransformMakeRotation(M_PI_2); // 90 degress
                    break;
                case UIInterfaceOrientationLandscapeRight:
                    yourview.transform = CGAffineTransformMakeRotation(M_PI + M_PI_2); // 270 degrees
                    break;
                case UIInterfaceOrientationPortraitUpsideDown:
                    yourview.transform = CGAffineTransformMakeRotation(M_PI); // 180 degrees
                    break;
                default:
                    yourview.transform = CGAffineTransformMakeRotation(0.0);
                    break;
            }
        }
    

相关问题