UIImageView zoom and pinch in UIScrollView

2020-03-31 17:58发布

问题:

I have tried many different ways of implementing expand image and pinch to zoom. I have found tutorials on how to do this with images saved in my project however I am retrieving my images from Firebase. I would like one image to take about half the ProfileViewController screen and the other pictures to be able to be expanded full page and also have the ability to zoom. My code I have for it now does this however the zooming and the position is not clean or easy to use.

var newImageView: UIImageView!



@IBAction func imageTapped(_ sender: UITapGestureRecognizer)  {
    let imageView = sender.view as! UIImageView

    let scrollView = UIScrollView(frame: self.view.frame)


    newImageView = UIImageView(image: imageView.image)
    newImageView.frame = self.view.frame
    newImageView.backgroundColor = .black
    newImageView.contentMode = .top
    newImageView.isUserInteractionEnabled = true


    let tap = UITapGestureRecognizer(target: self, action: #selector(dismissFullscreenImage))
    scrollView.addGestureRecognizer(tap)


    scrollView.delegate = self
    scrollView.minimumZoomScale = 1.0
    scrollView.maximumZoomScale = 3.0

    scrollView.addSubview(newImageView)

    self.view.addSubview(scrollView)
}

func viewForZooming(in scrollView: UIScrollView) -> UIView?
{
    return newImageView;
}

func dismissFullscreenImage(_ sender: UITapGestureRecognizer) {
    sender.view?.removeFromSuperview()
}

回答1:

Here is I am creating scrollview and imageview programatically and here img is a PHAsset that I am selecting image from gallery.

Here what I am doing, select an image and move to other view controller and I resize that image according to my imageview So image is not blurry after zooming it.

Minimum zooming is imageview bound and maxmimum zooming is 4.0.

func singleVC1()  {
        let v = UIView(frame: CGRect(x: 40, y: 50, width: self.view.frame.size.width - 80, height: view.frame.size.height - 200))
        let view1 = UIView(frame: CGRect(x: 0, y: 0, width: v.frame.size.width, height: v.frame.size.height/2 - 1))
        let scroll1 = UIScrollView.init(frame: view1.bounds)
        let imgview = UIImageView.init(frame: (CGRect)(origin: CGPoint.zero, size: self.returnAspectSize(targetWidth: view.frame.size.width, img: img)))
        self.setZoomScale(scrollview: scroll1, imageview: imgview)
        imgview.image = self.getUIImage(asset: img)
        scroll1.maximumZoomScale = 4.0;
        scroll1.delegate = self;
        scroll1.bounces = false
        scroll1.contentSize =  imgview.frame.size
        scroll1.addSubview(imgview)
        view1.layer.borderColor = UIColor.red.cgColor
        view1.layer.borderWidth = 5.0
        view1.addSubview(scroll1)
        v.addSubview(view1)

        let view2 = UIView(frame: CGRect(x: 0, y: v.frame.size.height/2+1, width: v.frame.size.width, height: v.frame.size.height/2 - 1))
        let scroll2 = UIScrollView.init(frame: view2.bounds)
        let imgview1 = UIImageView.init(frame: (CGRect)(origin: CGPoint.zero, size: self.returnAspectSize(targetWidth: view.frame.size.width, img: img)))
        imgview1.image = self.getUIImage(asset: img)
        self.setZoomScale(scrollview: scroll2, imageview: imgview1)
        scroll2.bounces = false

        scroll2.maximumZoomScale = 4.0;
        scroll2.delegate = self;
        view2.layer.borderColor = UIColor.red.cgColor
        view2.layer.borderWidth = 5.0
        scroll2.contentSize = imgview1.frame.size
        scroll2.addSubview(imgview1)
        view2.addSubview(scroll2)
        v.addSubview(view2)
        self.view.addSubview(v)
    }
func setZoomScale(scrollview : UIScrollView, imageview : UIImageView) {
        var minZoom = min(self.view.bounds.size.width / imageview.bounds.size.width, self.view.bounds.size.height / imageview.bounds.size.height);

        if (minZoom > 1.0) {
            minZoom = 1.0;
        }
        scrollview.minimumZoomScale = minZoom;
        scrollview.zoomScale = minZoom;
    }
    func returnAspectSize(targetWidth : CGFloat, img : PHAsset) -> CGSize {
        let img1 = self.getUIImage(asset: self.img)
        let targetwidth = targetWidth
        let scalefactor = (CGFloat)(targetwidth) / (CGFloat)((img1?.size.width)!)
        let targetheight = (img1?.size.height)! * scalefactor
        let targesize = CGSize(width: (CGFloat)(targetwidth), height: targetheight)
        return targesize
    }

Try this. I am sure this will help you.