如何在iOS上的CALayer中显示彩色图像?
我需要在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
我认为我的解决scheme不太麻烦:
let maskLayer = CALayer() maskLayer.frame = layer.bounds maskLayer.contents = tintedImage.CGImage layer.mask = maskLayer layer.backgroundColor = UIColor.redColor().CGColor
让我知道如果它工作正常:)
我想这是可能的使用CALayer的筛选器属性的OSX,但在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; }
我在网上find了这个片段,但是我不记得在哪里。
参考上面的评论。
我的代码:
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
应用解决scheme:
let maskLayer = CALayer()
最后:
layer.backgroundColor = UIColor.redColor().CGColor
结果是可怕的。
解决scheme是在下面的链接(评论中的链接)
但我显示我的代码(从链接):
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
请让我告诉你一些事情(很重要)不要玩弄:
- image@2x.png
- 或image@3x.png
图像名称“someImage.png”必须完全是您的项目文件夹中的文件名称。
- CALayers没有调整其UIView的边界改变。 为什么?
- 连续显示多个CALayer 60秒?
- 在AVCaptureVideoPreviewLayer中精确剪裁捕获的图像
- CALayer的抗混叠对angular线边缘
- setNeedsDisplay,drawRect或CALayer导致dispatch_continuation_alloc_from_heap malloc内存泄漏iOS
- CALayer Antialiasing不如UIView抗锯齿
- 在滚动后删除CALayer到UITableViewCell
- masksToBounds和cornerRadius之间的关系是什么?
- iOS的animation/变形形状从圆形到方形