Generating stripe lines using CoreImage - No sharp edges
Asked Answered
N

2

7

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

Negative answered 28/6, 2018 at 6:17 Comment(0)
T
6

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

Trover answered 11/7, 2018 at 17:39 Comment(0)
I
3

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))
Impostume answered 28/6, 2018 at 6:32 Comment(2)
I ll check it. what about the sharpness level I need to use here?Negative
not working for me. The image is intended to show as fill inside swift charts dataset.Negative

© 2022 - 2024 — McMap. All rights reserved.