Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将图像大小限制为ScrollView,Swift_Ios_Swift_Uiscrollview - Fatal编程技术网

Ios 将图像大小限制为ScrollView,Swift

Ios 将图像大小限制为ScrollView,Swift,ios,swift,uiscrollview,Ios,Swift,Uiscrollview,我在滚动视图中设置了一个图像,虽然我已经将滚动视图的框架设置为固定的高度和宽度,如下图所示,但图像超出了边界(见下图) 如何将图片限制在滚动视图中 imageScrollView.frame = CGRect(x: 0, y: 0, width: viewWidth, height: viewHeight-50) imageScrollView.clipsToBounds = true // Has no affect on the image 您是否有UIImageView的参考?如果是,则

我在滚动视图中设置了一个图像,虽然我已经将滚动视图的框架设置为固定的高度和宽度,如下图所示,但图像超出了边界(见下图)

如何将图片限制在滚动视图中

imageScrollView.frame = CGRect(x: 0, y: 0, width: viewWidth, height: viewHeight-50)
imageScrollView.clipsToBounds = true // Has no affect on the image

您是否有UIImageView的参考?如果是,则将其内容模式设置为aspect fit。像这样:

        theImageView.contentMode = .scaleAspectFit
您设置的clipsToBounds只覆盖子视图中超出父视图边界的任何部分,因此它对您没有任何帮助

或者,如果您使用的是Interface Builder,请设置此选项:


那么,如果您没有对UIImageView的引用呢?。。。 您可以迭代滚动视图的子视图,每当它找到UIImageView时,您就可以这样设置内容模式。比如:

//This is off the top of my head, so my filtering may not be right...
//This is also a one and done solution if you've got a lot of images in your scroll view
for anImgVw in imageScrollView.subviews.filter({$0.isKind(of: UIImageView.self)})
{
    anImgVw.contentMode = .scaleAspectFit
}

否则,我不确定是否可以不引用UIImageView。

是否有UIImageView的引用?如果是,则将其内容模式设置为aspect fit。像这样:

        theImageView.contentMode = .scaleAspectFit
您设置的clipsToBounds只覆盖子视图中超出父视图边界的任何部分,因此它对您没有任何帮助

或者,如果您使用的是Interface Builder,请设置此选项:


那么,如果您没有对UIImageView的引用呢?。。。 您可以迭代滚动视图的子视图,每当它找到UIImageView时,您就可以这样设置内容模式。比如:

//This is off the top of my head, so my filtering may not be right...
//This is also a one and done solution if you've got a lot of images in your scroll view
for anImgVw in imageScrollView.subviews.filter({$0.isKind(of: UIImageView.self)})
{
    anImgVw.contentMode = .scaleAspectFit
}

否则,我不确定在不引用UIImageView的情况下是否可以执行此操作。

您可以使用屏幕高度而不是视图高度

let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
let screenHeight = screenSize.height

imageScrollView.frame = CGRect(x: 0, y: 0, width: viewWidth, height: screenHeight-50)

您可以使用屏幕高度而不是视图高度

let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
let screenHeight = screenSize.height

imageScrollView.frame = CGRect(x: 0, y: 0, width: viewWidth, height: screenHeight-50)

您正在使用的库被编码为使缩放与设备方向相匹配。因此,如果图像方向与视图方向不匹配,则最终导致图像与滚动视图不太匹配

您需要编辑
ImageScrollView.swift
源文件。假设您使用的版本与当前提供的链接()中的版本相同,请按如下所示更改
setMaxMinZoomSalesForCurrentBounds()
函数:

fileprivate func setMaxMinZoomScalesForCurrentBounds() {
    // calculate min/max zoomscale
    let xScale = bounds.width / imageSize.width    // the scale needed to perfectly fit the image width-wise
    let yScale = bounds.height / imageSize.height   // the scale needed to perfectly fit the image height-wise

    // fill width if the image and phone are both portrait or both landscape; otherwise take smaller scale
    //let imagePortrait = imageSize.height > imageSize.width
    //let phonePortrait = bounds.height >= bounds.width
    //var minScale = (imagePortrait == phonePortrait) ? xScale : min(xScale, yScale)
    //
    // just take the min scale, so the image will completely fit regardless of orientation
    var minScale = min(xScale, yScale)

    let maxScale = maxScaleFromMinScale*minScale

    // don't let minScale exceed maxScale. (If the image is smaller than the screen, we don't want to force it to be zoomed.)
    if minScale > maxScale {
        minScale = maxScale
    }

    maximumZoomScale = maxScale
    minimumZoomScale = minScale * 0.999 // the multiply factor to prevent user cannot scroll page while they use this control in UIPageViewController
}

您正在使用的库被编码为使缩放与设备方向相匹配。因此,如果图像方向与视图方向不匹配,则最终导致图像与滚动视图不太匹配

您需要编辑
ImageScrollView.swift
源文件。假设您使用的版本与当前提供的链接()中的版本相同,请按如下所示更改
setMaxMinZoomSalesForCurrentBounds()
函数:

fileprivate func setMaxMinZoomScalesForCurrentBounds() {
    // calculate min/max zoomscale
    let xScale = bounds.width / imageSize.width    // the scale needed to perfectly fit the image width-wise
    let yScale = bounds.height / imageSize.height   // the scale needed to perfectly fit the image height-wise

    // fill width if the image and phone are both portrait or both landscape; otherwise take smaller scale
    //let imagePortrait = imageSize.height > imageSize.width
    //let phonePortrait = bounds.height >= bounds.width
    //var minScale = (imagePortrait == phonePortrait) ? xScale : min(xScale, yScale)
    //
    // just take the min scale, so the image will completely fit regardless of orientation
    var minScale = min(xScale, yScale)

    let maxScale = maxScaleFromMinScale*minScale

    // don't let minScale exceed maxScale. (If the image is smaller than the screen, we don't want to force it to be zoomed.)
    if minScale > maxScale {
        minScale = maxScale
    }

    maximumZoomScale = maxScale
    minimumZoomScale = minScale * 0.999 // the multiply factor to prevent user cannot scroll page while they use this control in UIPageViewController
}

设置imageview内容模式在内容模式中使用方面匹配查看此示例,例如newImgThumb.contentMode=.scaleSpectFit设置imageview内容模式在内容模式中使用方面匹配查看此示例,例如newImgThumb.contentMode=.scaleSpectFit我正在使用此库
https://github.com/huynguyencong/ImageScrollView
,当我尝试设置
zoomView=UIImageView(image:image)
zoomView?.contentMode=.ScaleSpectFit
时,它没有任何区别。@user44776-您使用的库旨在使缩放/滚动比视图大的图像变得容易。如果这不是您想要的,那么您就不应该使用该库。@DonMag,当加载库时,它应该在视图中显示图像,但该部分不起作用,双击时,它会缩放并可平移(这非常有效)。我正在使用该库
https://github.com/huynguyencong/ImageScrollView
,当我试图设置
zoomView=UIImageView(image:image)
zoomView?.contentMode=.ScaleSpectFit
时,这没有什么区别。@user44776-您使用的库旨在使缩放/滚动比视图大的图像变得容易。如果这不是您想要的,那么您不应该使用该库。@DonMag,当加载库时,它应该在视图中显示图像,但该部分不起作用,双击时,它会缩放并可平移(这非常有效)。