如何以编程方式在 SWIFT 中设置 UIImage 的 alpha?
How to set the alpha of an UIImage in SWIFT programmatically?
我在这里找到了很多解决方案,但不是 Swift,我知道你可以用 UIImageView 来做到这一点,但在我的例子中,我需要一个编程设置UIButton 的 alpha 透明背景图像。最好是 UIImage 扩展!
let img = UIImage(named: "imageWithoutAlpha")
var imgInsets = UIEdgeInsetsMake(0, 24, 0, 24)
image = image!.resizableImageWithCapInsets(imgInsets)
let myButton = UIButton(frame: CGRect(x: 50, y: 50, width: img!.size.width, height: img!.size.height))
myButton.setBackgroundImage(img, forState: UIControlState.Normal)
myButton.contentEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 20)
myButton.setTitle("Startbutton", forState: UIControlState.Normal)
myButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
myButton.sizeToFit()
view.addSubview(myButton)
当前结果:
想要的结果:
幸运的是我能够自救并想与您分享我的解决方案:
Swift 3
// UIImage+Alpha.swift
extension UIImage {
func alpha(_ value:CGFloat) -> UIImage {
UIGraphicsBeginImageContextWithOptions(size, false, scale)
draw(at: CGPoint.zero, blendMode: .normal, alpha: value)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
}
我将上面的新 Swift 扩展添加到我的项目中,然后我更改了 UIButton 示例,使其具有透明度为 50% 的 alpha 透明背景图像。
let img = UIImage(named: "imageWithoutAlpha")!.alpha(0.5)
let myButton = UIButton()
myButton.setBackgroundImage(img, for: .normal)
最简单的方法是将 UIImage
放在 UIImageView
中并在那里设置 alpha。
let image = UIImage(named: "imageWithoutAlpha")
let imageView = UIImageView(image: image)
imageView.alpha = 0.5
myButton.setBackgroundImage(image, forState: UIControlState.Normal)
我能够使用以下代码设置 alpha:
self.imageView.image = UIImageView(image: "image.png")
imageView.alpha = 0.5
Swift5,iOS10+
如果您的应用不支持 iOS10 之前的操作系统,那么您可以利用更强大的 UIGraphicsImageRenderer
UIKit 绘图 API.
extension UIImage {
func withAlpha(_ a: CGFloat) -> UIImage {
return UIGraphicsImageRenderer(size: size, format: imageRendererFormat).image { (_) in
draw(in: CGRect(origin: .zero, size: size), blendMode: .normal, alpha: a)
}
}
}
用法
let img = UIImage(named: "someImage")?.withAlpha(0.5)
我在这里找到了很多解决方案,但不是 Swift,我知道你可以用 UIImageView 来做到这一点,但在我的例子中,我需要一个编程设置UIButton 的 alpha 透明背景图像。最好是 UIImage 扩展!
let img = UIImage(named: "imageWithoutAlpha")
var imgInsets = UIEdgeInsetsMake(0, 24, 0, 24)
image = image!.resizableImageWithCapInsets(imgInsets)
let myButton = UIButton(frame: CGRect(x: 50, y: 50, width: img!.size.width, height: img!.size.height))
myButton.setBackgroundImage(img, forState: UIControlState.Normal)
myButton.contentEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 20)
myButton.setTitle("Startbutton", forState: UIControlState.Normal)
myButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
myButton.sizeToFit()
view.addSubview(myButton)
当前结果:
想要的结果:
幸运的是我能够自救并想与您分享我的解决方案:
Swift 3
// UIImage+Alpha.swift
extension UIImage {
func alpha(_ value:CGFloat) -> UIImage {
UIGraphicsBeginImageContextWithOptions(size, false, scale)
draw(at: CGPoint.zero, blendMode: .normal, alpha: value)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
}
我将上面的新 Swift 扩展添加到我的项目中,然后我更改了 UIButton 示例,使其具有透明度为 50% 的 alpha 透明背景图像。
let img = UIImage(named: "imageWithoutAlpha")!.alpha(0.5)
let myButton = UIButton()
myButton.setBackgroundImage(img, for: .normal)
最简单的方法是将 UIImage
放在 UIImageView
中并在那里设置 alpha。
let image = UIImage(named: "imageWithoutAlpha")
let imageView = UIImageView(image: image)
imageView.alpha = 0.5
myButton.setBackgroundImage(image, forState: UIControlState.Normal)
我能够使用以下代码设置 alpha:
self.imageView.image = UIImageView(image: "image.png")
imageView.alpha = 0.5
Swift5,iOS10+
如果您的应用不支持 iOS10 之前的操作系统,那么您可以利用更强大的 UIGraphicsImageRenderer
UIKit 绘图 API.
extension UIImage {
func withAlpha(_ a: CGFloat) -> UIImage {
return UIGraphicsImageRenderer(size: size, format: imageRendererFormat).image { (_) in
draw(in: CGRect(origin: .zero, size: size), blendMode: .normal, alpha: a)
}
}
}
用法
let img = UIImage(named: "someImage")?.withAlpha(0.5)