UIScroll View将初始图像大小设置为小于完整大小

时间:2015-04-20 04:39:01

标签: ios swift

此视图仅加载一个图像,并允许用户放大和缩小以近距离查看图像的某些部分。但是,它以巨大的尺寸加载,我希望它以屏幕尺寸加载。或者如果很难获得屏幕尺寸,那么只需加载50%的尺寸即可。

    override func viewDidLoad() {
    super.viewDidLoad()

// Do any additional setup after loading the view.

    self.scrollView.minimumZoomScale = 0
    self.scrollView.maximumZoomScale = 1
    self.scrollView.delegate = self
    self.imageView.image = self.image!
    }

    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
    return self.imageView
    }
}

如何将初始图像尺寸设置为合理的尺寸,但允许缩放到完整尺寸的图像?

谢谢!

2 个答案:

答案 0 :(得分:0)

获取屏幕尺寸:(目标-C)

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

将UIImageView框架设置为屏幕大小,然后将方面设置为适合:

imageView.contentMode = .ScaleAspectFit (UIViewContentModeScaleAspectFit in Obj-C)

然后,只需设置zoomScale:

self.scrollView.zoomScale = .5;

答案 1 :(得分:0)

您只需计算将视图宽度除以图像尺寸宽度的缩放,如下所示:

scrollView.zoomScale = view.frame.width / imageView.image.size.width