繁体   English   中英

快速地以编程方式创建动态高度UIImageViews

[英]creating dynamic height UIImageViews programmatically in swift

我想动态加载UIImage 大小不一。 下载图片后,我尝试执行的操作重置了UIImageView框架。

 let imgVw:UIImageView! = UIImageView.init(frame: CGRectMake(5, self.otherVidY, imgvwWidth, scrollH-10))
        imgVw.image=UIImage(named: "NotFound")
        self.scroll.addSubview(imgVw)

        let vwY = (self.otherVidY+scrollH-10)-30
        let view: UIView = UIView.init(frame: CGRectMake(5, vwY, imgvwWidth, 30))
        view.backgroundColor=UIColor.blackColor()
        self.scroll.addSubview(view)

        if let imgURL = NSURL(string: urlString)
        {
            do {
                if let img = imageCache[urlString] {
                    imgVw.image = img
                }

                else
                {
                    // The image isn't cached, download the img data
                    // We should perform this in a background thread
                    let request: NSURLRequest = NSURLRequest(URL: imgURL)
                    let mainQueue = NSOperationQueue.mainQueue()
                    NSURLConnection.sendAsynchronousRequest(request, queue: mainQueue, completionHandler: { (response, data, error) -> Void in
                        if error == nil {
                            // Convert the downloaded data in to a UIImage object
                            let image = UIImage(data: data!)
                            let resizedImage = self.resizeImage(image!, newWidth: imgVw.frame.size.width, imageView: imgVw)
                            // Store the image in to our cache
                            self.imageCache[urlString] = resizedImage
                            // Update the cell
                            dispatch_async(dispatch_get_main_queue(), {
                                imgVw.image = resizedImage
                                var rect = imgVw.frame
                                rect.size.height = resizedImage.size.height
                                imgVw.frame = rect
                                self.otherVidY = imgVw.frame.origin.y+imgVw.frame.size.height+5

                            })
                        }
                        else {
                            print("Error: \(error!.localizedDescription)")
                        }
                    })
                }

            }

        }

        self .otherVidY=imgVw.frame.size.height+imgVw.frame.origin.y+5
        self.scroll.contentSize=CGSizeMake(self.screenWidth, self.otherVidY+imgVw.frame.size.height)



    }

但是当我设置下一张图像的Y值时我遇到了一个问题。 其重叠。 如何解决这个问题。 请帮我。 谢谢

在这里,您必须处理起源,这仅取决于您的逻辑。 您可以使用。

object(View).frame.size.origin

它将为您提供X,Y视角和

view.frame.size.heightview.frame.size.width

它会给你身高和体重。

然后,需要为所需的ImageView设置CGRect。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM