我需要在CALayer中显示一个浅色图像。
适用于UIImageView:
imageView.image = image.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
但CALayer会以原始颜色显示图像,而不是色调。
let tintedImage = image.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
layer.contents = tintedImage.CGImage
有没有办法在CALayer中显示有色图像?
演示:
https://github.com/exchangegroup/calayer-with-tint-colored-image
答案 0 :(得分:22)
我认为我的解决方案不那么麻烦:
let maskLayer = CALayer()
maskLayer.frame = layer.bounds
maskLayer.contents = tintedImage.CGImage
layer.mask = maskLayer
layer.backgroundColor = UIColor.redColor().CGColor
让我知道它是否正常工作:)
答案 1 :(得分:3)
我想在OSX上可以使用CALayer的filter属性,但是在ios中没有使用。 我认为你应该完全重绘图像,这里是一个示例代码,它对所有具有alpha>的内容进行着色。 0
- (UIImage *)tintedImageWithColor:(UIColor *)tintColor blendingMode:(CGBlendMode)blendMode highQuality:(BOOL) yerOrNo;
{
UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f);
if (yerOrNo) {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetShouldAntialias(context, true);
CGContextSetAllowsAntialiasing(context, true);
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
}
[tintColor setFill];
CGRect bounds = CGRectMake(0, 0, self.size.width, self.size.height);
UIRectFill(bounds);
[self drawInRect:bounds blendMode:blendMode alpha:1.0f];
if (blendMode != kCGBlendModeDestinationIn)
[self drawInRect:bounds blendMode:kCGBlendModeDestinationIn alpha:1.0];
UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return tintedImage;
}
我在互联网上找到了这个片段,但我不记得在哪里。
答案 2 :(得分:0)
根据 J.Williams 的答案添加答案 - 但使用系统映像针对 Swift 5 进行了更新。包括所有图层设置等。希望这是有用的! (注意:我认为我们不能仅仅改变图层中 UIImage 的色调而不像这样跳过很多疯狂的圈子,这很荒谬)
<div class="range-slider">
<form method="get" action="{% url 'search' %}" class="d-flex align-items-center justify-content-between">
<div class="price-input">
<label for="amount">PriceFrom </label>
#Just Apply For loop for evry price form
{% for i in price form %}
<input type="number" name="price_from" value="{{price_from}}">
{% end for %}
</div>
<div class="price-input">
<label for="amount">PriceTo</label>
{% for i in price to %}
<input type="number" name="price_to" value="{{price_to}}">
{% end for %}
</div>
<button class="filter-btn">filter</button>
</form>
</div>
</div>
答案 3 :(得分:-1)
参考上面的评论。
我的代码:
button.backgroundColor = UIColor.withAlphaComponent(UIColor.white)(0.1)
button.layer.cornerRadius = button.frame.size.height / 2
button.layer.borderWidth = 3
button.layer.borderColor = UIColor.white.cgColor
button.layer.contents = UIImage(named: "buttonImage.png")?.cgImage
button.layer.contentsGravity = kCAGravityCenter
button.layer.magnificationFilter = kCAFilterLinear
button.layer.isGeometryFlipped = false
应用解决方案:
let maskLayer = CALayer()
最后:
layer.backgroundColor = UIColor.redColor().CGColor
结果太可怕了。
解决方案位于以下链接(评论中的链接)
但是我展示了我的代码(从链接中获取):
let image_ = UIImage(named: "image.png")
let maskImage = image_?.cgImage
let width = image_?.size.width
let height = image_?.size.height
let bounds = CGRect(x: 0, y: 0, width: width!, height: height!)
let colorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
let bitmapContext = CGContext(data: nil,
width: Int(width!),
height: Int(height!),
bitsPerComponent: 8,
bytesPerRow: 0,
space: colorSpace,
bitmapInfo: bitmapInfo.rawValue)
bitmapContext?.clip(to: bounds, mask: maskImage!)
bitmapContext?.setFillColor(UIColor.yellow.cgColor)
bitmapContext?.fill(bounds)
let cImage = bitmapContext?.makeImage()
let coloredImage = UIImage(cgImage: cImage!)
self.myUIButton.layer.contents = coloredImage.cgImage
请让我告诉你一些事情(很重要)不要玩:
图像名称“someImage.png”必须与项目文件夹中的文件名完全相同。