gpt4 book ai didi

ios - 未在 AsyncImageView 的子类中调用 drawRect

转载 作者:行者123 更新时间:2023-11-29 03:17:43 25 4
gpt4 key购买 nike

在我的项目中,我需要使用AsyncImageView 来加载图像。我还希望这些图像是圆形的。图像主要出现在表格 View 中,因此我无法在 AsyncImageView 层上使用圆角,因为这会对应用程序的性能产生非常非常负面的影响。

所以我决定创建一个 AsyncImageView 的子类(因为我项目中的其他 View 需要原始的非圆形 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 并将 ImageView 的 IBOutlet 更改为 RoundedAsyncImageView >。现在的问题是 drawRect 没有被调用。有什么想法吗?

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

最佳答案

UIImageView 上的 DrawRect 方法与 UIView 略有不同

The view still has a CALayer, but the layer doesn’t allocate a backing store. Instead it uses a CGImageRef as its contents and the render server will draw that image’s bits into the frame buffer.

In this case, there’s no drawing going on. We’re simply passing bitmap data in form of an image to the UIImageView.

引用这篇文章Getting Pixels onto the Screen ,根据我的经验,这就是为什么你的 imageView 剪辑方法无法工作。

这是我的解决方案:

  1. 不要创建 AsyncImageView 的子类,而是创建 UIView 的子类。
  2. 添加 AsyncImageView ,这意味着您的子类成为 AsyncImageView 的包装 View 。
  3. 在drawRect中实现clip方法,并让asyncImageView处理它的内容变化。

关于ios - 未在 AsyncImageView 的子类中调用 drawRect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21477739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com