drawRect未在AsyncImageView的子类中调用

时间:2014-01-31 10:29:11

标签: ios uiimageview drawrect rounded-corners

在我的项目中,我需要使用AsyncImageView来加载图片。我也希望那些图像是圆形的。图像主要出现在表格视图中,因此我无法在AsyncImageView's图层上使用roudned角落,因为这会对应用程序的性能产生非常非常不利的影响。

所以我决定创建一个AsyncImageView的子类(因为我需要在我的项目中为其他视图创建原始的非圆形AsyncImageView),称为RoundedAsyncImageView,我只在其中实现这样的drawRect方法:

- (void)drawRect:(CGRect)rect
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSaveGState(ctx);
    CGPathRef clippath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:17].CGPath;
    CGContextAddPath(ctx, clippath);
    CGContextClip(ctx);
    [self.image drawInRect:rect];
    CGContextRestoreGState(ctx);
}

我有这个圆形图像应该出现的单元格的xib,用IB创建。所以我只是将UIImageView元素的自定义类更改为RoundedAsyncImageView,将图像视图的IBOutlet更改为RoundedAsyncImageView。现在的问题是drawRect没有被调用。有什么想法吗?

编辑: 我读到UIImageView子类不调用drawRect,我应该简单地在常规UIView子类中绘制图像。问题是,我实际上无法做到这一点,因为我需要AsyncImageView的功能来异步加载图像。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:1)

DrawRect上的

UIImageView方法与UIView

略有不同
  

视图仍然有CALayer,但该图层不分配后备存储。相反,它使用CGImageRef作为其内容,渲染服务器将该图像的位绘制到帧缓冲区中。

     

在这种情况下,没有绘图。我们只是将图像形式的位图数据传递给UIImageView。

参考这篇文章Getting Pixels onto the Screen,根据我的经验,这就是为什么你无法使你的imageView剪辑方法有效。

这是我的解决方案:

  1. 创建AsyncImageView的子类,而不是创建UIView的子类。
  2. 添加AsyncImageView,这意味着您的子类成为AsyncImageView的包装器视图。
  3. 在drawRect中实现clip方法,让asyncImageView处理它的内容更改。
相关问题