当我的模态视图中出现图像时,我希望图像正确缩放,如下所示:
如果视图是横向的:
如果视图是肖像
我的肖像视图很好,因为我使用缩放比例适合纵向,这在肖像中非常有用
然而,无论我做什么,当我改变为风景时,我似乎无法正确地缩放图像/调整大小/位置。
提前谢谢。
(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]];}
答案 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
。
这是一个快速而肮脏的解决方案,但它适用于简单的场景,您只需要担心方面适合与宽高比填充。