There were no relevant replies, so to the best of my abilities I’ve put together a version that works:
public UIImage cropToCircle(UIImage inimg) {
if(null == inimg) return null;
// step 1. crop to a square:
var length = Math.Min(inimg.Size.Width, inimg.Size.Height);
var x = inimg.Size.Width/2 - length/2;
var y = inimg.Size.Height/2 - length/2;
var cropRect = new CGRect(x, y, length, length);
UIGraphics.BeginImageContextWithOptions(cropRect.Size, false, 0);
var context = UIGraphics.GetCurrentContext();
context.TranslateCTM(0.0f, (float)length);
context.ScaleCTM(1.0f, -1.0f);
context.DrawImage(new RectangleF(0, 0,
(float)length, (float)length), inimg.CGImage);
context.ClipToRect(cropRect);
var croppedImage = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
// step 2. crop to a circle
UIImageView imageView = new UIImageView(croppedImage);
var layer = imageView.Layer;
layer.MasksToBounds = true;
layer.CornerRadius =
(nfloat)(Math.Min(imageView.Frame.Height, imageView.Frame.Width)) / 2;
UIGraphics.BeginImageContext(imageView.Bounds.Size);
layer.RenderInContext(UIGraphics.GetCurrentContext());
var circleImg = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
return circleImg;
}
(The reason I’ve used a layer for circular cropping is I don’t know how to attach UIBezierPath to the context).
CLICK HERE to find out more related problems solutions.