UITableView中的圆形UIImageView没有性能损失?

swi*_*ode 81 objective-c uitableview uiimageview ios

我的UIImageView每个UITableView单元格上都有一个显示远程图像(使用SDWebImage)的图像.我已经QuartzCore为图像视图做了一些图层样式,如下所示:

UIImageView *itemImageView = (UIImageView *)[cell viewWithTag:100];
    itemImageView.layer.borderWidth = 1.0f;
    itemImageView.layer.borderColor = [UIColor concreteColor].CGColor;
    itemImageView.layer.masksToBounds = NO;
    itemImageView.clipsToBounds = YES;
Run Code Online (Sandbox Code Playgroud)

所以现在我有一个50x50的正方形,有一个微弱的灰色边框,但我想把它做成圆形而不是方形.该应用程序Hemoglobe在表视图中使用圆形图像,这就是我想要实现的效果.但是,我不想使用cornerRadius,因为这会降低我的滚动FPS.

这里Hemoglobe显示循环UIImageViews:

在此输入图像描述

有没有办法达到这个效果?谢谢.

tag*_*bek 140

只需将cornerRadius宽度或高度设置为一半(假设对象的视图为方形).

例如,如果对象的视图的宽度和高度均为50:

itemImageView.layer.cornerRadius = 25;
Run Code Online (Sandbox Code Playgroud)

更新 - 正如用户atulkhatri指出的那样,如果你不添加它将无法工作:

itemImageView.layer.masksToBounds = YES;
Run Code Online (Sandbox Code Playgroud)

  • 为什么这是最好的答案?好吧,我不这么认为.使用cornerRadius会导致滚动视图中的性能下降,尤其是在iPhone4设备中. (30认同)
  • 我喜欢'将cornerRadius设置为宽度或高度的一半',但不要忘记添加'itemImageView.layer.masksToBounds = YES;' 否则它将无法工作. (11认同)
  • 这应该修复你的tableview滚动性能问题.self.imageView.layer.shouldRasterize = YES; self.imageView.layer.rasterizationScale = [UIScreen mainScreen] .scale; (4认同)

yaz*_*azh 38

添加边框

self.ImageView.layer.borderWidth = 3.0f;

self.ImageView.layer.borderColor = [UIColor whiteColor].CGColor;
Run Code Online (Sandbox Code Playgroud)

对于圆形

self.ImageView.layer.cornerRadius = self.ImageView.frame.size.width / 2;
self.ImageView.clipsToBounds = YES;
Run Code Online (Sandbox Code Playgroud)

请参阅此链接

http://www.appcoda.com/ios-programming-circular-image-calayer/


shi*_*vam 14

使用此代码..这将有所帮助..

    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();
Run Code Online (Sandbox Code Playgroud)

这对我来说可以.:)


Ant*_*tzi 10

这是一个更新的方法,在swift中使用IBDesignable和IBInspectable来子类化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()
    }
}
Run Code Online (Sandbox Code Playgroud)


iPa*_*tel 7

是的,这可能给layer.cornerRadius (需要添加 #import <QuartzCore/QuartzCore.h>)
为创建循环的任何控制,但在你的情况,而不是一组layerUIImageView是创建自己的图像为圆形,并添加它的最佳方式UIImageView而曾经backGroundColorClearColor.

另请参阅这两个代码来源.

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

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

这可能对您的情况有所帮助:

  • 最后,我认为@iPatel是唯一关心这里表现的人.如果您想在滚动时获得高性能,请小心使用cornerRadius. (4认同)