Forum Xamarin.iOS

Circular shape image in Tableview

Hello guys,

I want to show profile image in Circular shape in tableview cell Xamarin iOS.But if i apply code for it.It turns either diamond shape or oval shape.May be because i'm applying image of differant dimensions.How can i show the image in Circle without losing the aspect ratio.

Answers

  • GeraldVersluisGeraldVersluis NLUniversity ✭✭✭✭
    edited November 2016

    Your best option is probably to use the Image Circle Plugin of look at FFImageLoading

  • BerayBentesenBerayBentesen TRUniversity ✭✭✭✭

    @UrvashiThakkar I use following code :

            CALayer imageCircle = YourUIImageView.Layer;
            imageCircle.CornerRadius = 30;
            imageCircle.MasksToBounds = true;
    
  • UrvashiThakkarUrvashiThakkar INMember ✭✭

    Hello @BerayBentesen ,

    I was using that code only,It didnt work in this case.So, i used instead this ::

    public static UIImage profileImage(this UIImage img)
    {

            var square = new CGSize(Math.Min(img.Size.Width,img.Size.Height), Math.Min(img.Size.Width, img.Size.Height));
    
             imageView = new UIImageView();
    
            imageView.Frame = new CGRect(new CGPoint(20, 20), square);
            imageView.ContentMode = UIViewContentMode.ScaleAspectFill;
            imageView.Image =img;
            imageView.Layer.CornerRadius = imageView.Frame.Width / 2;//square.Width / 2;
            imageView.Layer.BorderColor = UIColor.White.CGColor;
    
            imageView.Layer.MasksToBounds = true;
    
            UIGraphics.BeginImageContext(imageView.Bounds.Size);
            imageView.Layer.RenderInContext(UIGraphics.GetCurrentContext());
            var result = UIGraphics.GetImageFromCurrentImageContext();// UIGraphicsGetImageFromCurrentImageContext()
            UIGraphics.EndImageContext();
            return result;
        }
    

    The Image returned here is coming in Circle only without losing its aspect ratio.

Sign In or Register to comment.