Circular UIImageView in UITableView without performance hit?

前端 未结 14 2621
失恋的感觉
失恋的感觉 2020-12-12 09:50

I have a UIImageView on each of my UITableView cells, that display a remote image (using SDWebImage). I\'ve done some QuartzCore

相关标签:
14条回答
  • 2020-12-12 10:08

    Set the height & width of the of the UIImageView to be the same e.g.:Height=60 & Width = 60, then the cornerRadius should be exactly the half.I have kept it 30 in my case.It worked for me.

     self.imageView.layer.cornerRadius = 30;
     self.imageView.layer.borderWidth = 3;
     self.imageView.layer.borderColor = [UIColor whiteColor].CGColor;
     self.imageView.layer.masksToBounds = YES;
    
    0 讨论(0)
  • 2020-12-12 10:12

    Use this code.. This will be helpful..

        UIImage* image = ...;
        UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0);
        // Add a clip before drawing anything, in the shape of an rounded rect
        [[UIBezierPath bezierPathWithRoundedRect:imageView.bounds
                                    cornerRadius:50.0] addClip];
        // Draw your image
        [image drawInRect:imageView.bounds];
    
        // Get the image, here setting the UIImageView image
        imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    
        // Lets forget about that we were drawing
        UIGraphicsEndImageContext();
    

    It works fine for me. :)

    0 讨论(0)
  • 2020-12-12 10:12

    Usable solution in Swift using extension:

    extension UIView{
      func circleMe(){
          let radius = CGRectGetWidth(self.bounds) / 2
          self.layer.cornerRadius = radius
          self.layer.masksToBounds = true
      }
    }
    

    Usage:

    self.venueImageView.circleMe()
    
    0 讨论(0)
  • 2020-12-12 10:12

    In swift, inside your viewDidLoad method, with the userImage outlet:

    self.userImage.layer.borderWidth = 1;
    self.userImage.layer.borderColor = UIColor.whiteColor().CGColor;
    self.userImage.layer.cornerRadius = self.userImage.frame.size.width / 2;
    self.userImage.clipsToBounds = true;
    
    0 讨论(0)
  • 2020-12-12 10:12

    For circular imageview, use the below code.

    self.imageView.layer.cornerRadius = self.imageView.frame.size.width / 2;
    self.imageView.clipsToBounds = true
    

    Don't forget to change the cornerRadius in viewDidLayoutSubviews method of your UIView.

    Example:

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        self.imageView.layer.cornerRadius = self.imageView.frame.size.width / 2;
        self.imageView.clipsToBounds = true
    }
    
    0 讨论(0)
  • 2020-12-12 10:13

    Yes, it is possible to give layer.cornerRadius (need to add #import <QuartzCore/QuartzCore.h>)
    for create circular any control but in your case instead of set layer of UIImageView it is The Best Way to Create Your Image as circular and add it on UIImageView Which have backGroundColor is ClearColor.

    Also refer this Two Source of code.

    https://www.cocoacontrols.com/controls/circleview

    and

    https://www.cocoacontrols.com/controls/mhlazytableimages

    This might be helpful in your case:

    0 讨论(0)
提交回复
热议问题