gpt4 book ai didi

ios - UIImageView 大小大于 Table View 内设置的框架

转载 作者:行者123 更新时间:2023-11-30 12:49:17 27 4
gpt4 key购买 nike

我有一个tableview,它有一个profileView标题。

lazy var tableView : UITableView = {
let tv = UITableView()
tv.delegate = self
tv.translatesAutoresizingMaskIntoConstraints = false
tv.tableHeaderView = self.profileView
tv.dataSource = self
tv.backgroundColor = .clear
tv.register(ReviewTableViewCell.self, forCellReuseIdentifier: ReviewTableViewCell.reuseIdentifier)
tv.separatorStyle = .none
return tv
}()

lazy var profileView : UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()

在 profileView/tableview 标题中,我有一个名为 avatarImage 的图像。

var avatarImage:UIImageView = {
let iv = UIImageView()
iv.translatesAutoresizingMaskIntoConstraints = false
iv.backgroundColor = .blue
iv.layer.cornerRadius = 10.0
iv.layer.borderColor = UIColor.white.cgColor
iv.layer.borderWidth = 3.0
iv.contentMode = UIViewContentMode.scaleAspectFit
return iv
}()

override func viewWillLayoutSubviews() {
setupViews()
setupProfileView()

if let imageURL = selectedStylist?.imageUrl {
avatarImage.loadImageUsingCacheWithUrlString(imageURL)
}
}

我给头像图片一个框架

func setupViews() {

view.addSubview(tableView)
NSLayoutConstraint.activate([

tableView.topAnchor.constraint(equalTo: self.view.topAnchor),
tableView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
tableView.leftAnchor.constraint(equalTo: self.view.leftAnchor),
tableView.rightAnchor.constraint(equalTo: self.view.rightAnchor),
])

profileView.addSubview(avatarImage)

avatarImage.frame = CGRect(origin: CGPoint(x: view.frame.width/2 - 80/2 ,
y: yPositionOfAvatarImage),
size: CGSize(width: 80,
height: 80))
}

这是用于将图像从 url 加载到 imageView 的扩展

let imageCache = NSCache<NSString, UIImage>()

extension UIImageView {

func loadImageUsingCacheWithUrlString(_ urlString: String) {

self.image = nil

//check cache for image first
if let cachedImage = imageCache.object(forKey: urlString as NSString){
self.image = cachedImage
return
}

//otherwise fire off a new download
guard let url = URL(string: urlString) else {
return
}
URLSession.shared.dataTask(with: url) {
data, response, error in

//download hit an error so lets return out
if error != nil {

return
}
DispatchQueue.main.async(execute: {
if let downloadedImage = UIImage(data: data!) {
imageCache.setObject(downloadedImage, forKey: urlString as NSString)
self.image = downloadedImage
}
})
}.resume()
}

}

图像不是 80x80 并适合 avatarImage,而是 avatarImage 似乎填满了屏幕,并且我相信,它是获取的图像的大小而不是 imageView 的大小。如果获取的图像是1024*680,则imageView的大小将为1024*680而不是80*80。

最佳答案

I give the avatarImage a frame

但是这一行:

iv.translatesAutoresizingMaskIntoConstraints = false

的意思是“忽略我给这个 View 的框架!使用约束来代替。”好吧,使用约束的规则是默认情况下 ImageView 是其图像的大小。

关于ios - UIImageView 大小大于 Table View 内设置的框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41208553/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com