gpt4 book ai didi

swift - 如何以编程方式快速使 UItabbar 图像四舍五入

转载 作者:可可西里 更新时间:2023-10-31 23:57:35 25 4
gpt4 key购买 nike

我想创建如下图所示的东西你能帮我任何代码如何使它四舍五入吗感谢您的帮助!

enter image description here

最佳答案

我认为你需要

  1. (可选)如果图像大于标准条形按钮图像大小,则调整图像大小)
  2. 使图像变圆

在将其用作 UItabBarItem 图像之前。下面是具有这两个功能的 UIImage 的扩展:

extension UIImage{
var roundedImage: UIImage {
let rect = CGRect(origin:CGPoint(x: 0, y: 0), size: self.size)
UIGraphicsBeginImageContextWithOptions(self.size, false, 1)
UIBezierPath(
roundedRect: rect,
cornerRadius: self.size.height
).addClip()
self.draw(in: rect)
return UIGraphicsGetImageFromCurrentImageContext()!
}
func resizedImage(newWidth: CGFloat) -> UIImage {
let scale = newWidth / self.size.width
let newHeight = self.size.height * scale
UIGraphicsBeginImageContext(CGSize(width: newWidth, height: newHeight))
self.draw(in: CGRect(x: 0, y: 0, width: newWidth, height: newHeight))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

return newImage!
}
}

示例用法:

let barImage: UIImage = UIImage(named: "avatar_copy.png")!.resizedImage(newWidth: 40).roundedImage.withRenderingMode(.alwaysOriginal)
let roundTabBar = UITabBarItem(title: nil, image: barImage.withRenderingMode(.alwaysOriginal), selectedImage: barImage)
self.bottomTabbar.items = [roundTabBar]

关于swift - 如何以编程方式快速使 UItabbar 图像四舍五入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45228342/

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