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
}
What I need is below
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:
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With