This is my simple function I use for drawing an image in context:
let renderer=UIGraphicsImageRenderer(size: CGSize(width: 330, height: 330))
let img=renderer.image{ ctx in
let circle=CGRect(x:0,y:0,width: 330, height: 330)
ctx.cgContext.setFillColor(UIColor.white.cgColor)
ctx.cgContext.addEllipse(in: circle)
ctx.cgContext.drawPath(using: .fill)
let image = UIImage(named: "1")!
image.draw(in: CGRect(x: 80, y: 80, width: 100, height: 100))
}
And the result is following:
As you can see there is output of UIGraphicsImageRenderer
with border around ellipse. Why? Border is not defined anywhere, but it is printed.
The image named 1
is the following one:
NOTE:
This issue appears only when compiling ios app. Using playground everything is fine and ok.
2
Answers
OK, the updated code still does not match.First, in your posted image, the background is not white.
Second, even accounting for that, there is no "edge" on the rendered
UIImage
.So, I’m going to make a guess here….
Assuming you execute the
img = renderer.image { ....
code block, and then you setimageView.image = img
, my suspicion is that you have something like this:So, the lightGray "circle" is the lightGray background anti-aliased to the
.cornerRadius
.I would be that if set:
and do not set the layer’s cornerRadius (no need to), your ellipse border will be gone.
If it’s still there, then you need to provide some code that actually reproduces the issue.
Edit
I’m still not seeing the "border" when setting the rendered image to an image view, but…
Doing some debug inspecting and using the "1" image you added, there IS a difference.
Try this, and see if it gets rid of the border:
You can then use either:
or Rob’s suggested:
Does your
UIImageView
have acornerRadius
applied to its layer? That can cause a thin gray border like you see here. If you create a circular image, like you have withUIGraphicsImageRenderer
, you should not need to do any masking orcornerRadius
on theUIImageView
.If you only want to fill the path, and not stroke it, one could use
fillPath
rather thandrawPath
.FWIW, you could also just bypass the CoreGraphics context and just
fill
the oval directly: