Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating stripe lines using CoreImage - No sharp edges

Im generating image with strip lines using CoreImage framework and the output seems to have improper edges as shown in the below image. Code I have used is as below.

fileprivate func generateImage(_ width: CGFloat, _ height: CGFloat, withColor1: CIColor, withColor2: CIColor) -> CGImage? {

   let context = CIContext()

   if #available(iOS 10.0, *) {
   let stripes = CIFilter(name: "CIStripesGenerator", withInputParameters: [
                "inputColor0" : withColor2,
                "inputColor1" : withColor1,
                "inputWidth" : NSNumber(value: 10),
                "inputSharpness" : NSNumber(value: 0.7)

                ])!.outputImage!

    let rotate = CIFilter(name: "CIAffineTransform", withInputParameters: [ "inputImage" : stripes,"inputTransform" : NSValue(cgAffineTransform: CGAffineTransform(rotationAngle: -75.0))])!.outputImage!

    return context.createCGImage(rotate, from: CGRect(x: 0, y: 0, width: width, height: 50.0))!

        } else {
            // Fallback on earlier versions
        }

        return nil

    }

With high sharpness

enter image description here

With low sharpness(0.7)

enter image description here

What I need is below

enter image description here

like image 524
Saranjith Avatar asked Jun 28 '18 06:06

Saranjith


2 Answers

Give radian, not degree, value .pi / -4 to the rotationAngle: of CGAffineTransform.

import UIKit
import CoreImage

fileprivate func
generateImage(_ width: CGFloat, _ height: CGFloat, withColor1: CIColor, withColor2: CIColor) -> CGImage? {

    let context = CIContext()

    let stripes = CIFilter(
        name: "CIStripesGenerator"
    ,   parameters: [
            "inputColor0"   : withColor2
        ,   "inputColor1"   : withColor1
        ,   "inputWidth"    : NSNumber( value: 10 )
        ,   "inputSharpness": NSNumber( value: 1 )
        ]
    )!.outputImage!

    let rotate = CIFilter(
        name: "CIAffineTransform"
    ,   parameters: [
            "inputImage"    : stripes
        ,   "inputTransform": NSValue( cgAffineTransform: CGAffineTransform( rotationAngle: .pi / -4 ) )
        ]
    )!.outputImage!

    return context.createCGImage( rotate, from: CGRect( x: 0, y: 0, width: width, height: height ) )!
}


class ViewController: UIViewController {
    @IBOutlet   weak    var oIV:    UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
        oIV.image = UIImage(
            cgImage: generateImage(
                oIV.bounds.width
            ,   oIV.bounds.height
            ,   withColor1: CIColor( red:0, green:0, blue:0, alpha:1 )
            ,   withColor2: CIColor( red:1, green:1, blue:1, alpha:1 )
            )!
        )
    }
}

results: enter image description here

like image 192
Satachito Avatar answered Nov 11 '22 16:11

Satachito


You could try something like this, however I haven't tested it properly:

let generatedImage = generateImage(...)

let scaleX = yourView.frame.size.width / generatedImage.extent.size.width
let scaleY = yourView.frame.size.height / generatedImage.extent.size.height

let transformedImage = generatedImage.transformed(by: CGAffineTransform(scaleX: scaleX, y: scaleY))
like image 42
fulvio Avatar answered Nov 11 '22 17:11

fulvio