swift – 如何在从带有翠鸟的URL加载图像后调整UIImageView的大小

swift – 如何在从带有翠鸟的URL加载图像后调整UIImageView的大小,第1张

概述我想调整所有下载的图像的大小,以便它们保持纵横比,但是它们都与它们呈现的UITableViewCell一样宽. 我的UIImageView配置了contentMode作为AspectFit,我的单元格上有以下锚点: 当前的尝试几乎就在那里: class AnimatedImageTableViewCell: UITableViewCell { @IBOutlet weak var gifV 我想调整所有下载的图像的大小,以便它们保持纵横比,但是它们都与它们呈现的UItableVIEwCell一样宽.

我的UIImageVIEw配置了contentMode作为AspectFit,我的单元格上有以下锚点:

当前的尝试几乎就在那里:

@H_301_17@class AnimatedImagetableVIEwCell: UItableVIEwCell { @IBOutlet weak var gifVIEw: AnimatedImageVIEw! @IBOutlet weak var imageHeightAnchor: NSLayoutConstraint! var refreshCell: (() -> VoID)? func render(imageUrl: String) { guard let url = URL(string: imageUrl) else { return } gifVIEw.kf.setimage(with: url) { result in switch result { case .success(let value): let ratio = value.image.size.wIDth / value.image.size.height let newHeight = self.gifVIEw.frame.wIDth / ratio self.imageHeightAnchor.constant = newHeight self.refreshCell?() case .failure(let error): print(error) // The error happens } } }}

也:

@H_301_17@class HomeVIEwController: UItableVIEwController { let images = [ "https://cdn-images-1.medium.com/max/1600/1*OJxJTJLSyqJ0nMeuswuCSQ.gif","https://static1.squarespace.com/static/552a5cc4e4b059a56a050501/565f6b57e4b0d9b44ab87107/566024f5e4b0354e5b79dd24/1449141991793/NYCGifathon12.gif","https://media2.giphy.com/avatars/100soft/WahNEDdlGjRZ.gif" ] overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() tableVIEw.tableFooterVIEw = UIVIEw() tableVIEw.estimatedRowHeight = 200 tableVIEw.rowHeight = UItableVIEw.automaticDimension tableVIEw.allowsSelection = false let nib = UINib(nibname: "AnimatedImagetableVIEwCell",bundle: nil) tableVIEw.register(nib,forCellReuseIDentifIEr: "cellID") } overrIDe func tableVIEw(_ tableVIEw: UItableVIEw,numberOfRowsInSection section: Int) -> Int { return images.count } overrIDe func tableVIEw(_ tableVIEw: UItableVIEw,cellForRowAt indexPath: IndexPath) -> UItableVIEwCell { let imagePath = images[indexPath.item] let cell = tableVIEw.dequeueReusableCell(withIDentifIEr: "cellID",for: indexPath) as! AnimatedImagetableVIEwCell cell.refreshCell = { tableVIEw.reloadRows(at: [indexPath],with: .automatic) } cell.render(imageUrl: imagePath) return cell }}

但是在某些情况下,细胞太高了.图像呈现全宽,正确的宽高比,图像上方和下方的空间太大.

我也在控制台中出现如下错误:

@H_301_17@( "<NSLayoutConstraint:0x600000329770 Kingfisher.AnimatedImageVIEw:0x7fd339e19330.height == 268.5 (active)>","<NSLayoutConstraint:0x60000032a8f0 Kingfisher.AnimatedImageVIEw:0x7fd339e19330.top == UItableVIEwCellContentVIEw:0x7fd339e1a280.topmargin + 8 (active)>","<NSLayoutConstraint:0x60000032a850 UItableVIEwCellContentVIEw:0x7fd339e1a280.bottommargin == Kingfisher.AnimatedImageVIEw:0x7fd339e19330.bottom + 8 (active)>","<NSLayoutConstraint:0x60000032a7b0 'UIVIEw-bottommargin-guIDe-constraint' V:[UILayoutGuIDe:0x600001908ee0'UIVIEwLayoutmarginsGuIDe']-(8)-| (active,names: '|':UItableVIEwCellContentVIEw:0x7fd339e1a280 )>","<NSLayoutConstraint:0x600000323980 'UIVIEw-Encapsulated-Layout-Height' UItableVIEwCellContentVIEw:0x7fd339e1a280.height == 450.5 (active)>","<NSLayoutConstraint:0x60000032a6c0 'UIVIEw-topmargin-guIDe-constraint' V:|-(8)-[UILayoutGuIDe:0x600001908ee0'UIVIEwLayoutmarginsGuIDe'] (active,names: '|':UItableVIEwCellContentVIEw:0x7fd339e1a280 )>")

解决方法 您可以在设置其图像属性后尝试调整图像视图的大小.例如:

@H_301_17@class ResizableImageVIEw: UIImageVIEw { overrIDe var image: UIImage? { dIDSet { guard let image = image else { return } let resizeConstraints = [ self.heightAnchor.constraint(equalToConstant: image.size.height),self.wIDthAnchor.constraint(equalToConstant: image.size.wIDth) ] if supervIEw != nil { addConstraints(resizeConstraints) } } }}

然后,添加其常规约束,省略heightAnchor和wIDthAnchor,因为这些将在添加图像后立即添加(我建议此时不使用界面构建器):

@H_301_17@let imageVIEw = ResizableImageVIEw(frame: .zero)imageVIEw.translatesautoresizingMaskIntoConstraints = falsevIEw.addSubvIEw(imageVIEw)// These are only illustrative constraintsimageVIEw.centerXAnchor.constraint(equalTo: vIEw.centerXAnchor).isActive = trueimageVIEw.centerYAnchor.constraint(equalTo: vIEw.centerYAnchor).isActive = trueimageVIEw.image = image

这是一个完整且功能齐全的示例:

@H_301_17@class VIEwController: UIVIEwController { overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() downloadImage( fromURL: URL(string: "https://img.evbuc.com/https%3A%2F%2Fcdn.evbuc.com%2Fimages%2F33105753%2F184176199496%2F1%2Foriginal.jpg?h=200&w=450&rect=0%2C0%2C400%2C200&s=a92a5a03c153d312d682b2dfedd6a6ad")!,completionHandler: { [weak self] image in guard let self = self,let image = image else { return } let imageVIEw = ResizableImageVIEw(frame: .zero) imageVIEw.translatesautoresizingMaskIntoConstraints = false self.vIEw.addSubvIEw(imageVIEw) imageVIEw.centerXAnchor.constraint(equalTo: self.vIEw.centerXAnchor).isActive = true imageVIEw.centerYAnchor.constraint(equalTo: self.vIEw.centerYAnchor).isActive = true imageVIEw.image = image }) } func downloadImage(fromURL url: URL,completionHandler: @escaPing (UIImage?) -> VoID) { let operationQueue = OperationQueue() operationQueue.addOperation { guard let data = try? Data(contentsOf: url) else { OperationQueue.main.addOperation { completionHandler(nil) } return } OperationQueue.main.addOperation { let image = UIImage(data: data) completionHandler(image) } } }} 总结

以上是内存溢出为你收集整理的swift – 如何在从带有翠鸟的URL加载图像后调整UIImageView的大小全部内容,希望文章能够帮你解决swift – 如何在从带有翠鸟的URL加载图像后调整UIImageView的大小所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1015469.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存