[英]UIImageView size is bigger than the frame set inside Table View
I have a tableview, which has a header of profileView. 我有一个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
}()
Inside the profileView/tableview header I have an image named avatarImage. 在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)
}
}
I give the avatarImage a frame 我给avatarImage一个框架
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))
}
This is the extension used to load the image from the url to the imageView 这是用于将图像从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()
}
}
Instead of the image being 80x80 and fitting inside avatarImage, the avatarImage seems to fill the screen and, I believe, be the size of the image fetched rather than the size of the imageView. 图像不是80x80并适合放置在avatarImage内,而avatarImage似乎填满了屏幕,并且我相信是获取的图像的大小而不是imageView的大小。 If the image fetched is 1024*680, the size of the imageView will be 1024*680 rather than 80*80. 如果获取的图像为1024 * 680,则imageView的大小将为1024 * 680,而不是80 * 80。
I give the avatarImage a frame 我给avatarImage一个框架
But this line: 但是这一行:
iv.translatesAutoresizingMaskIntoConstraints = false
means "Ignore the frame I give this view! Use constraints instead." 表示“忽略我给出的视图框架!改为使用约束。” Well, using constraints the rule is that by default an image view is the size of its image. 好吧,使用约束的规则是默认情况下,图像视图是其图像的大小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.