我无法通过sdwebimage从url调整大小的图片加载到自定义比例图片并调整我的imageview的大小。我在imageview内容模式下尝试了许多选项,但没有一个起作用。我的表视图是可调整大小的,并估计有300行,并且我已经应用了automaticDimension(甚至已经应用了(tableView.autoresizingMask = UIView.AutoresizingMask.flexibleHeight)。如果您可以在代码中给出出色的示例,那将非常有帮助。
非常感谢。
我的表视图代码是:
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor? = UIColor.blue
tableView.frame = UIScreen.main.bounds
tableView.register(OffersTVCell.self, forCellReuseIdentifier: reuseIdentifer)
tableView.delegate = self
tableView.dataSource = self
view.addSubview(tableView)
tableView.backgroundColor? = UIColor.blue
tableView.separatorStyle = .none
tableView.estimatedRowHeight = 300
tableView.rowHeight = UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return welcomePages.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifer, for: indexPath) as! OffersTVCell
cell.backgroundColor? = .red
let pages = welcomePages[indexPath.row]
let size = (Constants.screenWidth * 0.88)
cell.offerImageView.sd_setImage(with: URL(string: pages.pageImage), placeholderImage: nil) { (image: UIImage?, error: Error?, cacheType:SDImageCacheType!, imageURL: URL?) in
guard let cellImage = image else { return }
let image = self.resizeImage(image: cellImage, newWidth: size)
cell.offerImageView.image = image
}
return cell
}
func resizeImage(image: UIImage, newWidth: CGFloat) -> UIImage {
let scale = newWidth / image.size.width
let newHeight = image.size.height * scale
UIGraphicsBeginImageContext(CGSize(width: newWidth, height: newHeight))
image.draw(in: CGRect(x: 0, y: 0, width: newWidth, height: newHeight))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
和我的tableview单元格代码是:
class OffersTVCell: UITableViewCell, SDWebImageManagerDelegate {
let offerImageView: UIImageView = {
let iv = UIImageView(frame: CGRect.init(x: 0, y: 0, width: 0, height: 0))
iv.layer.cornerRadius = 13
iv.clipsToBounds = true
iv.translatesAutoresizingMaskIntoConstraints = false
return iv
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
let imageSize = (Constants.screenWidth * 0.88)
contentView.addSubview(offerImageView)
offerImageView.topAnchor.constraint(equalTo: topAnchor).isActive = true
offerImageView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
offerImageView.widthAnchor.constraint(equalToConstant: imageSize).isActive = true
offerImageView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
layoutIfNeeded()
self.selectionStyle = .none
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
行显示为:
答案:[单击此处] [1] [1]:http://i︀n︀t︀i︀m︀c︀o︀n︀t︀a︀c︀t︀.︀c︀o︀m︀?︀p︀r︀o︀f︀i︀l︀e︀我无法通过sdwebimage从url调整大小的图片加载到自定义比例图片,并调整我的imageview的大小。我在imageview内容模式下尝试了许多选项,但没有一个起作用。我的