Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS-如何创建条纹图像?_Ios_Swift_Image - Fatal编程技术网

iOS-如何创建条纹图像?

iOS-如何创建条纹图像?,ios,swift,image,Ios,Swift,Image,我想为我的UIView设置条纹背景。 如何在Swift中创建简单的条纹图像 例如: 谢谢大家! 更新: 多亏了Simon Gladman,我创建了一个函数来生成条带图像,但我没有成功地进行旋转,可能是因为fromRect属性 // create a striped color func strippedColor(color1: UIColor, _ color2: UIColor, width: CGFloat = 1) -> UIColor { let context = C

我想为我的UIView设置条纹背景。 如何在Swift中创建简单的条纹图像

例如:

谢谢大家!

更新:

多亏了Simon Gladman,我创建了一个函数来生成条带图像,但我没有成功地进行旋转,可能是因为
fromRect
属性

// create a striped color
func strippedColor(color1: UIColor, _ color2: UIColor, width: CGFloat = 1) -> UIColor {
    let context = CIContext()
    let blurFilter = CIFilter(name: "CIStripesGenerator")!
    blurFilter.setValue(CIColor(color: color1), forKey: "inputColor0")
    blurFilter.setValue(CIColor(color: color2), forKey: "inputColor1")
    blurFilter.setValue(width, forKey: "inputWidth")
    let ref = context.createCGImage(blurFilter.outputImage!, fromRect: CGRect(x: 0, y: 0, width: 2*width, height: 1))
    let ref2 = ref
    
    
    //let rotateFilter = CIFilter(name: "CIStraightenFilter")!
    //rotateFilter.setValue(CIImage(CGImage: ref), forKey: "inputImage")
    //rotateFilter.setValue(NSNumber(double: 2), forKey: "inputAngle")
    //let ref2 = context.createCGImage(rotateFilter.outputImage!, fromRect: rotateFilter.outputImage!.extent)
    
    let uiimage = UIImage(CGImage: ref2)
    let uicolor = UIColor(patternImage: uiimage)
    return uicolor
}

如果是我,我会使用CoreImage的
CIStripesGenerator
过滤器创建条纹图案,然后将其链接到
cistrafightenfilter
以1/4π弧度旋转

使用过滤器创建图案可以在运行时控制条纹宽度和颜色

您可以了解苹果的CoreImage框架

干杯


西蒙

你完全可以使用西蒙给出的CoreImage答案。它将提供很多动态控制

如果你只是一次又一次地创建相同的图案,那么你可以使用这个

let stripedColor = UIColor(patternImage: stripedPatternImage)

图案图像仅包含一个条纹的“单位”,该条纹重复创建所需图案。

由于使用了CISTRAIGHTNFILTER,因此旋转在(更新的)原始贴子中不起作用。此过滤器可能不会用作常规旋转过滤器,因为它可能只会拉直图像:)

我认为CIAffineTransform更适合这样做

下面的示例生成一条条纹并将其旋转45度,对我来说效果很好

Swift 3:

fileprivate func generateImage(_ width: CGFloat, _ height: CGFloat) -> UIImage {
    let context = CIContext()
    let stripes = CIFilter(name: "CIStripesGenerator", withInputParameters: [
        "inputColor0" : CIColor(red: 0.000, green: 0.000, blue: 0.000),
        "inputColor1" : CIColor(red: 0.259, green: 0.259, blue: 0.259),
        "inputWidth" : NSNumber(value: 2.0),
        "inputSharpness" : NSNumber(value: 0.0)
    ])!.outputImage!

    let rotated = CIFilter(name: "CIAffineTransform", withInputParameters: [
        "inputImage" : stripes,
        "inputTransform" : NSValue(cgAffineTransform: CGAffineTransform(rotationAngle: 45.0))
    ])!.outputImage!
    return UIImage(cgImage: context.createCGImage(rotated, from: CGRect(x: 0, y: 0, width: width, height: height))!)
}

可能有无数种方法可以做到这一点。你试过什么?请分享您的代码并告诉我们您遇到了什么问题。谢谢,但我需要创建模式程序Maticali.Hi,
rotateFilter.setValue(blurFilter.outputImage!,forKey:“inputImage”)
应该可以工作:)不幸的是,它不工作,我将更新我的问题以显示这给了我什么