Circular UIImageView in UITableView without performance hit?

前端 未结 14 2622
失恋的感觉
失恋的感觉 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:14

    Simply set the cornerRadius to half of the width or height (assuming your object's view is square).

    For example, if your object's view's width and height are both 50:

    itemImageView.layer.cornerRadius = 25;
    

    Update - As user atulkhatri points out, it won't work if you don't add:

    itemImageView.layer.masksToBounds = YES;
    
    0 讨论(0)
  • 2020-12-12 10:14

    In Swift use this Extension for CircularImageView or RoundedImageView :

    extension UIView {
    
        func circular(borderWidth: CGFloat = 0, borderColor: UIColor = UIColor.whiteColor()) {
            let radius = CGRectGetWidth(self.bounds) / 2
            self.layer.cornerRadius = radius
            self.layer.masksToBounds = true
    
            self.layer.borderWidth = borderWidth
            self.layer.borderColor = borderColor.CGColor
        }
    
        func roundedCorner(borderWidth: CGFloat = 0, borderColor: UIColor = UIColor.whiteColor()) {
            let radius = CGRectGetWidth(self.bounds) / 2
            self.layer.cornerRadius = radius / 5
            self.layer.masksToBounds = true
    
            self.layer.borderWidth = borderWidth
            self.layer.borderColor = borderColor.CGColor
        }
    
    }
    

    Usage:

    self.ImageView.circular()
    self.ImageView.roundedCorner()
    
    0 讨论(0)
  • 2020-12-12 10:20

    If you showing the images over a solid background color, an easy solution could be to use an overlay image with a transparent circle in the middle.

    This way you can still use square images and add the circle image above them to get the circular effect.

    If you don't need to manipulate your images or show them on a complex background color, this can be a simple solution with no performance hit.

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

    Here is a more up to date method in swift using IBDesignable and IBInspectable to subclass UIImageView

    @IBDesignable class RoundableUIImageView: UIImageView {
        private var _round = false
        @IBInspectable var round: Bool {
            set {
                _round = newValue
                makeRound()
            }
            get {
                return self._round
            }
        }
        override internal var frame: CGRect {
            set {
                super.frame = newValue
                makeRound()
            }
            get {
                return super.frame
            }
    
        }
    
        private func makeRound() {
            if self.round == true {
                self.clipsToBounds = true
                self.layer.cornerRadius = (self.frame.width + self.frame.height) / 4
            } else {
                self.layer.cornerRadius = 0
            }
        }
    
        override func layoutSubviews() {
                makeRound()
        }
    }
    
    0 讨论(0)
  • 2020-12-12 10:21

    If use some autolayout and different cells heights, u better do it this way:

       override func layoutSubviews() {
            super.layoutSubviews()
            logo.layer.cornerRadius = logo.frame.size.height / 2;
            logo.clipsToBounds      = true;
        }
    
    0 讨论(0)
  • 2020-12-12 10:21

    I use a Round Image View class… So I just use it instead of UIImageView, and have nothing to tweak…

    This class also draws an optional border around the circle. There is often a border around rounded pictures.

    It is not a UIImageView subclass because UIImageView has its own rendering mechanism, and does not call the drawRect method..

    Interface :

    #import <UIKit/UIKit.h>
    
    @interface MFRoundImageView : UIView
    
    @property(nonatomic,strong) UIImage* image;
    
    @property(nonatomic,strong) UIColor* strokeColor;
    @property(nonatomic,assign) CGFloat strokeWidth;
    
    @end
    

    Implementation :

    #import "MFRoundImageView.h"
    
    
    @implementation MFRoundImageView
    
    -(void)setImage:(UIImage *)image
    {
        _image = image;
        [self setNeedsDisplay];
    }
    
    -(void)setStrokeColor:(UIColor *)strokeColor
    {
        _strokeColor = strokeColor;
        [self setNeedsDisplay];
    }
    
    -(void)setStrokeWidth:(CGFloat)strokeWidth
    {
        _strokeWidth = strokeWidth;
        [self setNeedsDisplay];
    }
    
    - (void)drawRect:(CGRect)rect {
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGPathRef path = CGPathCreateWithEllipseInRect(self.bounds, NULL);
        CGContextAddPath(ctx, path);
        CGContextClip(ctx);
        [self.image drawInRect:rect];
    
        if ( ( _strokeWidth > 0.0f ) && _strokeColor ) {
            CGContextSetLineWidth(ctx, _strokeWidth*2); // Half border is clipped
            [_strokeColor setStroke];
            CGContextAddPath(ctx, path);
            CGContextStrokePath(ctx);
        }
        CGPathRelease(path);
    }
    
    @end
    
    0 讨论(0)
提交回复
热议问题