如何进行照片缩放,设置宽高比然后缩放在Swift中留下白色区域? [英] How to make a photo zoom, setting aspect ratio and then zooming leaves white area in Swift?

查看:138
本文介绍了如何进行照片缩放,设置宽高比然后缩放在Swift中留下白色区域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[滚动后:问题白屏] 1

缩放后

[缩放图像设置宽高比之前]

我正在实现一张照片查看页面,其中仅包含一张照片。功能:缩放。

I am implementing a photo view page, which includes just a photo. Functionalities : Zoom.

由于图像尺寸为1024x510。我已经实现了宽高比并将imageview框架设置为屏幕尺寸。我已经实现了使用Scrollview和imageview概念。

As the image is of size 1024x510. I have implemented aspect ratio and set the imageview frame to the screensize. I have implemented using Scrollview and imageview concept.

问题是,当我缩放图像时,白屏也会被缩放。问题是缩放时的空白视图。

代码

The problem is that, when I zoom the image white screen is also zoomed. Problem is the white space os imageview while zooming. . Code

override func viewDidLoad() {
super.viewDidLoad()



print(imageIndex)
self.scrollV.maximumZoomScale = 5.0
self.scrollV.minimumZoomScale = 1.0
 self.scrollV.contentSize = self.view.bounds.size;
self.scrollV.delegate = self

let urlStr : NSString = vehicleImages[imageIndex].stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!




url=NSURL(string: urlStr as String)
imageView.kf_setImageWithURL(url,placeholderImage: UIImage(named: "placeholder.png"))

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

}


推荐答案

看看这些方法。可能会有所帮助。我有滚动视图拉伸到控制器的视图大小。 customizeScrollView()将计算最小和最大比例选项。 centerImageView()将UIImageView置于UIScrollView的中心

Take a look at these methods. May be it will help. I have scroll view stretched to controller's view size. customizeScrollView() will calculate min and max scale options. centerImageView() will put UIImageView in the center of your UIScrollView

fileprivate func customizeScrollView() {
    guard let image = imageView?.image else { return }
    var minZoom = fmin(self.view.frame.width / image.size.width, self.view.frame.height / image.size.height)
    minZoom = fmin(1.0, minZoom)

    scrollView?.contentSize = image.size
    scrollView?.minimumZoomScale = minZoom
    scrollView?.addSubview(self.imageView!)
    scrollView?.setZoomScale(minZoom, animated: false)
    centerImageView()
}

fileprivate func centerImageView() {
    guard let imageView = imageView else { return }
    guard let scrollView = scrollView else { return }

    let boundsSize = scrollView.bounds.size
    var frameToCenter = imageView.frame

    // Center horizontally
    if frameToCenter.size.width < boundsSize.width {
        frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2
    } else {
        frameToCenter.origin.x = 0
    }

    // Center vertically
    if frameToCenter.size.height < boundsSize.height {
        frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2
    } else {
        frameToCenter.origin.y = 0
    }

    imageView.frame = frameToCenter
}

另外UIScrollViewDelegate方法:

Also UIScrollViewDelegate methods:

/// `UIScrollViewDelegate` protocol implementation
extension ImagePreviewController: UIScrollViewDelegate {

    public func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return imageView
    }

    public func scrollViewDidZoom(_ scrollView: UIScrollView) {
        centerImageView()
    }
}

这篇关于如何进行照片缩放,设置宽高比然后缩放在Swift中留下白色区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆