swift – 创建透明渐变并将其用作SpriteKit中的alpha掩码
我正在尝试制作渐变并将其用作alpha蒙版.现在,我能够制作一个类似于此的图像(从黑色到透明):
这是我用来制作所有这些的代码: private func createImage(width: CGFloat,height: CGFloat) -> CGImageRef?{ if let ciFilter = CIFilter(name: "CILinearGradient"){ let ciContext = CIContext() ciFilter.setDefaults() let startColor = CIColor(red: 0,green: 0,blue: 0,alpha: 0) let endColor = CIColor(red: 0,alpha: 1) let startVector = CIVector(x: 0,y: height-10) let endVector = CIVector(x: 0,y: height-22) ciFilter.setValue(startColor,forKey: "inputColor0") ciFilter.setValue(endColor,forKey: "inputColor1") ciFilter.setValue(startVector,forKey: "inputPoint0") ciFilter.setValue(endVector,forKey: "inputPoint1") if let outputImage = ciFilter.outputImage { let cgImage:CGImageRef = ciContext.createCGImage(outputImage,fromRect: CGRect(x: 0,y: 0,width: width,height: height)) return cgImage } } return nil } 对我来说,这种方式非常完美,因为我在代码中只创建了一次掩码(创建GUI元素时),因此性能根本不会受到影响. 问题是我实际上需要一个结果图像(渐变纹理)看起来像这样(渐变应该有固定的高度,但黑色区域的大小可能会有所不同): 因为CILinearGradient过滤器没有inputImage参数,所以我不能一个接一个地链接两个过滤器.但我必须创建一个图像,并应用过滤器,然后创建另一个图像,并应用新的过滤器. 我解决了这个就像上面描述的那样.此外,我不得不分三步扩展一个过程(创建上部渐变,创建中间,黑色区域并创建较低的渐变),然后将所有这些组合成一个图像. 我想知道有什么关于CILinearGradient过滤器我不知道的?也许有一种更简单的方法可以解决这个问题并制作复杂的渐变? 请注意,我正在使用SpriteKit并使用CAGradientLayer解决这个问题并不是我想要的方式. 解决方法
我不确定Core Image是否适合这里的工作 – 使用CGGradient函数绘制图像可能会更好.
旁白:你所要求的是可能的,但在CoreImage中很麻烦.可以想象一下,作为Photoshop或其他常用图形软件的用户创建这些图像所做的工作…… CoreImage的方式是这样的: >为上部渐变创建图层,使用渐变填充整个画布,然后将图层转换为正确的位置和大小 您可以使用生成器过滤器,变换过滤器和合成过滤器的组合来完成此操作…… gradient -> transform - }-> composite - solid color -> transform -/ }-> composite gradient -> transform ————————————————--/ 但是设置它会很难看. 同样,在Photoshop中,您可以使用选择工具和填充/渐变工具在单个图层内完全创建渐变填充渐变设计……这就是使用CoreGraphics绘制单个图像的类似内容. 那么,如何做单通CG绘图?像这样…… func createImage(width: CGFloat,_ height: CGFloat) -> CGImage { let gradientOffset: CGFloat = 10 // white at bottom and top before/after gradient let gradientLength: CGFloat = 12 // height of gradient region // create colors and gradient for drawing with let space = CGColorSpaceCreateDeviceRGB() let startColor = UIColor.whiteColor().CGColor let endColor = UIColor.blackColor().CGColor let colors: CFArray = [startColor,endColor] let gradient = CGGradientCreateWithColors(space,colors,[0,1]) // start an image context UIGraphicsBeginImageContext(CGSize(width: width,height: height)) defer { UIGraphicsEndImageContext() } let context = UIGraphicsGetCurrentContext() // fill the whole thing with white (that'll show through at the ends when done) CGContextSetFillColorWithColor(context,startColor) CGContextFillRect(context,CGRect(x: 0,height: height)) // draw top gradient CGContextDrawLinearGradient(context,gradient,CGPoint(x: 0,y: gradientOffset),y: gradientOffset + gradientLength),[]) // fill solid black middle CGContextSetFillColorWithColor(context,endColor) CGContextFillRect(context,y: gradientOffset + gradientLength,height: height - 2 * gradientOffset - 2 * gradientLength)) // draw bottom gradient CGContextDrawLinearGradient(context,y: height - gradientOffset),y: height - gradientOffset - gradientLength),[]) return CGBitmapContextCreateImage(context)! } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- ruby-on-rails – Rails:如何从上传显示图像
- ruby – HomeBrew Mac os x 10.8抛出错误“没有这
- Oracle sequence increased unexpected when usi
- postgresql-9.1 – postgresql中十进制数字和数字
- c# – 检测Windows错误/弹出窗口
- PostgreSql数据库的神器 FDW
- ruby-on-rails – 在rails 3.2.8中未定义read_in
- c# – DateTime.ParseExact给出String未被识别为
- 【转】正则表达式匹配行的开始'^'和结尾
- Flex【原创】模拟Android图案解锁