iOS: 如何将 TabBar 的 UIImage 中的特定颜色替换为另一种颜色(select 和 unselect)?
iOS: How to replace a specific color by another in a UIImage of a TabBar (select and unselect)?
我想在我的 UITabBar 中用另一种颜色替换选中的颜色。目的是获得一张我可以通过编程更改颜色的图像。
我该怎么做?
let tabBarItem = UITabBarItem(title: menuName, image: unselectedImage, selectedImage: selectedImage)
我检查了那张票没有成功:
link1, link2
我遇到了同样的问题。你应该创建一个背景为白色的 png,并将另一个图像放在上面
具有半透明背景 png。
(我认为你应该使用质量更好的图像,因为你的照片看起来很奇怪)
let iconWhite: UIImage? = self.load(fileName: "APP/icones/\(iconName).png")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
//UNSELECT IMAGE
var unselectedImgBack = UIImage(named: "white.png")
var unselectedImgFront = iconWhite?.overlayImage(color: UIColor.black)
let size = unselectedImgFront?.size
unselectedImgBack = unselectedImgBack?.resized(to: size!)
UIGraphicsBeginImageContext(size!)
let areaSize = CGRect(x: 0, y: 0, width: size!.width, height: size!.height)
unselectedImgBack!.draw(in: areaSize)
unselectedImgFront!.draw(in: areaSize, blendMode: .normal, alpha: 1)
unselectedImgFront = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
let unselectedImage = unselectedImgFront!.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
//SELECT IMAGE
var selectedImgBack = UIImage(named: "white.png")
var selectedImgFront = iconWhite
let scanner2 = Scanner(string: color)
var value: UInt64 = 0
if scanner2.scanHexInt64(&value) {
print("Decimal: \(value)")
print("Hex: 0x\(String(value, radix: 16))")
}
selectedImgBack = selectedImgBack?.overlayImage(color: UIColor.init(rgb: Int(value)))
selectedImgBack = selectedImgBack?.resized(to: size!)
UIGraphicsBeginImageContext(size!)
selectedImgBack!.draw(in: areaSize)
selectedImgFront!.draw(in: areaSize, blendMode: .normal, alpha: 0.8)
selectedImgFront = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
let selectedImage = selectedImgFront!.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
我想在我的 UITabBar 中用另一种颜色替换选中的颜色。目的是获得一张我可以通过编程更改颜色的图像。
我该怎么做?
let tabBarItem = UITabBarItem(title: menuName, image: unselectedImage, selectedImage: selectedImage)
我检查了那张票没有成功:
link1, link2
我遇到了同样的问题。你应该创建一个背景为白色的 png,并将另一个图像放在上面 具有半透明背景 png。
(我认为你应该使用质量更好的图像,因为你的照片看起来很奇怪)
let iconWhite: UIImage? = self.load(fileName: "APP/icones/\(iconName).png")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
//UNSELECT IMAGE
var unselectedImgBack = UIImage(named: "white.png")
var unselectedImgFront = iconWhite?.overlayImage(color: UIColor.black)
let size = unselectedImgFront?.size
unselectedImgBack = unselectedImgBack?.resized(to: size!)
UIGraphicsBeginImageContext(size!)
let areaSize = CGRect(x: 0, y: 0, width: size!.width, height: size!.height)
unselectedImgBack!.draw(in: areaSize)
unselectedImgFront!.draw(in: areaSize, blendMode: .normal, alpha: 1)
unselectedImgFront = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
let unselectedImage = unselectedImgFront!.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
//SELECT IMAGE
var selectedImgBack = UIImage(named: "white.png")
var selectedImgFront = iconWhite
let scanner2 = Scanner(string: color)
var value: UInt64 = 0
if scanner2.scanHexInt64(&value) {
print("Decimal: \(value)")
print("Hex: 0x\(String(value, radix: 16))")
}
selectedImgBack = selectedImgBack?.overlayImage(color: UIColor.init(rgb: Int(value)))
selectedImgBack = selectedImgBack?.resized(to: size!)
UIGraphicsBeginImageContext(size!)
selectedImgBack!.draw(in: areaSize)
selectedImgFront!.draw(in: areaSize, blendMode: .normal, alpha: 0.8)
selectedImgFront = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
let selectedImage = selectedImgFront!.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)