Thursday, May 21, 2026
HomeiOS Developmentios - add rounding for the define swift

ios – add rounding for the define swift

[ad_1]

I’ve a code that provides an overview of the required width and colour to a picture and it really works effectively
However I nonetheless want to regulate the rounding for the contour, please inform me during which path ought to I begin?

There are two capabilities right here that do the job of laying the define.

  func colorized(with colour: UIColor = .white) -> UIImage {
    UIGraphicsBeginImageContextWithOptions(dimension, false, scale)

    defer {
        UIGraphicsEndImageContext()
    }

    guard let context = UIGraphicsGetCurrentContext(), let cgImage = cgImage else { return self }


    let rect = CGRect(x: 0, y: 0, width: dimension.width, top: dimension.top)

    colour.setFill()
    context.translateBy(x: 0, y: dimension.top)
    context.scaleBy(x: 1.0, y: -1.0)
    context.clip(to: rect, masks: cgImage)
    context.fill(rect)

    guard let coloured = UIGraphicsGetImageFromCurrentImageContext() else { return self }

    return coloured
}

/**
Returns the stroked model of the fransparent picture with the given stroke colour and the thickness.

- Parameters:
    - colour: The colours to person. By defaut, makes use of the ``UIColor.white`
    - thickness: the thickness of the border. Default to `2`
    - high quality: The variety of levels (out of 360): the smaller the perfect, however the slower. Defaults to `10`.

- Returns: the stroked model of the picture, or self if one thing was flawed
*/

func stroked(with colour: UIColor = .crimson, thickness: CGFloat = 2, high quality: CGFloat = 10) -> UIImage {

    guard let cgImage = cgImage else { return self }

    // Colorize the stroke picture to mirror border colour
    let strokeImage = colorized(with: colour)

    guard let strokeCGImage = strokeImage.cgImage else { return self }

    /// Rendering high quality of the stroke
    let step = high quality == 0 ? 10 : abs(high quality)

    let oldRect = CGRect(x: thickness, y: thickness, width: dimension.width, top: dimension.top).integral
    let newSize = CGSize(width: dimension.width + 2 * thickness, top: dimension.top + 2 * thickness)
    let translationVector = CGPoint(x: thickness, y: 0)


    UIGraphicsBeginImageContextWithOptions(newSize, false, scale)

    guard let context = UIGraphicsGetCurrentContext() else { return self }

    defer {
        UIGraphicsEndImageContext()
    }
    context.translateBy(x: 0, y: newSize.top)
    context.scaleBy(x: 1.0, y: -1.0)
    context.interpolationQuality = .excessive

    for angle: CGFloat in stride(from: 0, to: 360, by: step) {
        let vector = translationVector.rotated(round: .zero, byDegrees: angle)
        let remodel = CGAffineTransform(translationX: vector.x, y: vector.y)

        context.concatenate(remodel)

        context.draw(strokeCGImage, in: oldRect)

        let resetTransform = CGAffineTransform(translationX: -vector.x, y: -vector.y)
        context.concatenate(resetTransform)
    }

    context.draw(cgImage, in: oldRect)

    guard let stroked = UIGraphicsGetImageFromCurrentImageContext() else { return self }

    return stroked
}

I attempted many choices however a lot of the solutions are simply including a conerRadius to the UIImageView (this does not work)

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments