如何重新调整图像以使其成比例并适合方向变化?

时间:2013-12-02 18:07:53

标签: ios iphone image ios7 imageview

当我的模态视图中出现图像时,我希望图像正确缩放,如下所示:

如果视图是横向的:

  • 如果是横向图像,则应该适合屏幕边界
  • 如果是肖像图像,则应居中

如果视图是肖像

  • 如果是纵向图像适合垂直中心
  • 相同的风景图片

我的肖像视图很好,因为我使用缩放比例适合纵向,这在肖像中非常有用

然而,无论我做什么,当我改变为风景时,我似乎无法正确地缩放图像/调整大小/位置。

提前谢谢。

 (void)viewDidLoad
  {

[super viewDidLoad];

self.scollView.delegate=self;

UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];

[doubleTap setNumberOfTapsRequired:2];

[self.viewImage addGestureRecognizer:doubleTap];

}

- (void)resetContentMode:(UIImageView *)imageView
   {
   if (imageView.image == nil)
    return;

CGSize imageSize = imageView.image.size;
CGSize imageViewSize = imageView.bounds.size;

if (imageSize.height == 0 || imageViewSize.height == 0)
    return;

CGFloat imageRatio        = imageSize.width     / imageSize.height;
CGFloat imageViewRatio    = imageViewSize.width / imageViewSize.height;
CGFloat percentDifference = fabs(imageRatio - imageViewRatio) / imageViewRatio;

if (percentDifference < 0.25)
    imageView.contentMode = UIViewContentModeScaleAspectFill;
else
    imageView.contentMode = UIViewContentModeScaleAspectFit;
}






      -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer   shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer    *)otherGestureRecognizer

{          返回YES;    }

 - (BOOL)shouldAutorotate {
  return YES;
  }

 - (NSUInteger)supportedInterfaceOrientations {
   return UIInterfaceOrientationMaskAll;
  } 
          -(void)willAnimateRotationToInterfaceOrientation:     (UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
  {
       if(toInterfaceOrientation == UIInterfaceOrientationPortrait){
        [self resetContentMode:im];
    }
      else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight ||      toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {




        [self resetContentMode:im];
     }
  }


   - (void) adjustViewsForOrientation:(UIInterfaceOrientation) orientation {
[self.scollView setZoomScale:self.scollView.minimumZoomScale animated:YES];
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{

    [self resetContentMode:im];
}
   else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{

     [self resetContentMode:im];

      }


   }
    - (void)orientationChanged:(NSNotification *)notification{
   [self adjustViewsForOrientation:[[UIApplication sharedApplication]    statusBarOrientation]];}

1 个答案:

答案 0 :(得分:0)

如果您不需要双指缩放,而只需根据图像的方向是否与图像视图的方向匹配来调整图像视图的外观,则可以在此过程中退出滚动视图。 / p>

您只需使用图片视图即可完成所需的效果,但可以根据图像尺寸与图像视图的尺寸比例的接近程度来更改contentMode。如果它们接近,我将默认为UIViewContentModeScaleAspectFill(并且由于比率彼此接近,我不必担心被剪裁太多)。如果它们没有关闭,我将默认为UIViewContentModeScaleAspectFit(因为比率不接近,我知道如果我使用“填充”,很多会被剪裁,所以我默认为“适合”)。

- (void)resetContentMode:(UIImageView *)imageView
{
    if (imageView.image == nil)
        return;

    CGSize imageSize = imageView.image.size;
    CGSize imageViewSize = imageView.bounds.size;

    if (imageSize.height == 0 || imageViewSize.height == 0)
        return;

    CGFloat imageRatio        = imageSize.width     / imageSize.height;
    CGFloat imageViewRatio    = imageViewSize.width / imageViewSize.height;
    CGFloat percentDifference = fabs(imageRatio - imageViewRatio) / imageViewRatio;

    if (percentDifference < 0.25)
        imageView.contentMode = UIViewContentModeScaleAspectFill;
    else
        imageView.contentMode = UIViewContentModeScaleAspectFit;
}

然后,每当我旋转时,再次调用此方法。例如:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    [self resetContentMode:self.imageView];
}

我还在图片上打开userInteractionEnabled,并在两种内容模式之间切换双击手势:

- (void)handleDoubleTap:(UITapGestureRecognizer *)gesture
{
    UIImageView *imageView = (id)gesture.view;

    if (imageView.contentMode == UIViewContentModeScaleAspectFit)
        imageView.contentMode = UIViewContentModeScaleAspectFill;
    else if (imageView.contentMode == UIViewContentModeScaleAspectFill)
        imageView.contentMode = UIViewContentModeScaleAspectFit;
    else
        [self resetContentMode:imageView];
}

我还要确保UIImageView将其clipsToBounds设置为YES,这样当它使用UIViewContentModeScaleAspectFill时,我不必担心图像流失图片视图的bounds

这是一个快速而肮脏的解决方案,但它适用于简单的场景,您只需要担心方面适合与宽高比填充。