首页 文章

在swift中在UIImageView上创建缩放

提问于
浏览
0

我目前正在从我的服务器加载图像 . 图像很多,具体取决于它返回的图像数量 . 我已经能够成功显示这些图像,我可以向下滚动查看所有图像 . 现在我的问题是我想要一种方法,我可以点击特定的图像,我将能够缩放它 . 我在下面分享了我的代码:

//This line of code downloads the images and displays them
for obj in message{
                                if let dict = obj as? NSDictionary {
                                   self.imagePath = dict.value(forKey: "path") as? String

                                    let albumThumbnail = photos()
                                    self.scrollView.addSubview(albumThumbnail)
                                    albumThumbnail.translatesAutoresizingMaskIntoConstraints = false
                                    albumThumbnail.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor).isActive = true
                                    albumThumbnail.topAnchor.constraint(equalTo: self.scrollView.topAnchor, constant: contentViewTopConstraint).isActive = true
                                    albumThumbnail.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor).isActive = true
                                    albumThumbnail.heightAnchor.constraint(equalToConstant: 150).isActive = true
                                    albumThumbnail.isUserInteractionEnabled = true


                                    if let path = self.path{
                                        if let imagePath = self.imagePath{
                                            let strippedPath = path.replacingOccurrences(of: "\\", with: "")
                                            let strippedImagePath = imagePath.replacingOccurrences(of: "\\", with: "")

                                            albumThumbnail.sd_setImage(with: URL(string: "\(strippedPath)\(strippedImagePath)"), placeholderImage: UIImage(named: "default_profile"), options: [.continueInBackground, .progressiveDownload])
                                        }
                                    }

                                    contentViewTopConstraint = contentViewTopConstraint + 170
                                }
                            }

1 回答

  • 0

    在图像点击时,U创建了滚动视图并将相应的图像添加为子视图,然后缩放就可以了 .

    var getClickImage = UIImageView()
    var zoomscrollV = UIScrollView()
    
    override func viewDidLoad() {
            super.viewDidLoad()
    zoomscrollV.delegate = self
    }
    
    override func viewDidAppear(_ animated: Bool) {
            zoomscrollV.isHidden = true
            zoomscrollV.frame = CGRect(x:0, y:0, width:self.view.frame.width, height:self.view.frame.height - 50)
            zoomscrollV.minimumZoomScale=1
            zoomscrollV.maximumZoomScale=10
            zoomscrollV.bounces=false
    
            self.view.addSubview(zoomscrollV)
    
            getClickImage=UIImageView()
    
            getClickImage.frame = CGRect(x:0, y:0, width:zoomscrollV.frame.width, height:zoomscrollV.frame.height)
            getClickImage.backgroundColor = .black
            getClickImage.contentMode = .scaleAspectFit
            zoomscrollV.addSubview(getClickImage)
    }
    
    
    // ON CLICKING IMAGE ACTION
    @objc func myImageTapped(_ sender: UITapGestureRecognizer) {
    
        zoomscrollV.isHidden = false
    
        // RESPECTIVE IMAGE
        getClickImage.image = BG_CARD_IMGS[(sender.view?.tag)!] 
    
        let closeButton: UIButton = UIButton(type: .custom)
        closeButton.frame = CGRect(x:40.0, y:self.view.frame.height - 50, width:self.view.frame.width - 80, height:50.0)
        closeButton.addTarget(self, action: #selector(self.closeZoom), for: .touchUpInside)
        closeButton.setTitle("CLOSE ZOOM", for: .normal)
        closeButton.setTitleColor(UIColor.red, for: .normal)
    
        // CLOSE BUTTON 
        self.view.addSubview(closeButton)
    
    }
    
    @objc func closeZoom(sender: AnyObject) {
        zoomscrollV.isHidden = true
        zoomscrollV.setZoomScale(1.0, animated: false)
        sender.removeFromSuperview()
    }
    
    //SCROLLVIEW DELEGATE
    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
    
        return getClickImage
    
    }
    

    OUTPUT

相关问题