我们为什么不使用UIImageView的子类?

时间:2023-02-09 10:02:37

I'm currently trying to create a subclass of UIImageView in order to make it download its image from server asynchronously ;)

我目前正在尝试创建UIImageView的子类,以使它从服务器异步下载它的图像;

I tried to do it by myself but I haven't gone very far yet :D

我试着自己做这件事,但我还没有走得很远

Anyway, I looked around here and found this :

不管怎样,我环顾四周发现:

AsyncImageDownload

AsyncImageDownload

I had a look at the code and the first which springs to mind is : why subclassing a UIView and not a UIImageView ?!?

我看了一下代码,首先想到的是:为什么子类化UIView而不是UIImageView ?

Any thoughts ?

任何想法吗?

Cheers mates,

欢呼声伴侣,

Gotye.

Gotye。

2 个解决方案

#1


1  

The reason it is subclassing UIView is so that you should, for example, display a UIActivityIndicator while the image is being downloaded. They do not show this in their example but I have used this code and it is really good. Also look at the comments for this post you will find more code examples, also including some caching and nice stuff.

它子类化UIView的原因是,例如,你应该在图像被下载时显示一个UIActivityIndicator。他们没有在他们的例子中展示这一点,但是我使用了这个代码,它真的很好。还可以查看本文的注释,您将发现更多的代码示例,包括一些缓存和一些不错的东西。

Take a look at Wayne Cochra's comment. His YellowJacket.zip code is very nice.

看看韦恩·科克拉的评论。他的YellowJacket。邮政编码很好。

#2


2  

A part of the Gang of Fours design pattern philosophy is to

“*”设计模式哲学的一部分就是去

"Favor 'object composition' over 'class inheritance'."

“支持‘对象组合’而不是‘类继承’。”

This reduces the tight coupling between ojects. Then changing one class will have less impact on the other classes in the system. This makes changes easier, resulting in a more stable, easy to maintain system.

这就减少了注入器之间的紧密耦合。然后,更改一个类将对系统中的其他类影响较小。这使得更改更容易,从而使系统更稳定、更易于维护。

In this case, as a previous poster mentioned, it allows the image to do other things as well, such as display a progress indicator.

在这种情况下,正如前面提到的海报,它允许图像做其他事情,比如显示进度指示器。

#1


1  

The reason it is subclassing UIView is so that you should, for example, display a UIActivityIndicator while the image is being downloaded. They do not show this in their example but I have used this code and it is really good. Also look at the comments for this post you will find more code examples, also including some caching and nice stuff.

它子类化UIView的原因是,例如,你应该在图像被下载时显示一个UIActivityIndicator。他们没有在他们的例子中展示这一点,但是我使用了这个代码,它真的很好。还可以查看本文的注释,您将发现更多的代码示例,包括一些缓存和一些不错的东西。

Take a look at Wayne Cochra's comment. His YellowJacket.zip code is very nice.

看看韦恩·科克拉的评论。他的YellowJacket。邮政编码很好。

#2


2  

A part of the Gang of Fours design pattern philosophy is to

“*”设计模式哲学的一部分就是去

"Favor 'object composition' over 'class inheritance'."

“支持‘对象组合’而不是‘类继承’。”

This reduces the tight coupling between ojects. Then changing one class will have less impact on the other classes in the system. This makes changes easier, resulting in a more stable, easy to maintain system.

这就减少了注入器之间的紧密耦合。然后,更改一个类将对系统中的其他类影响较小。这使得更改更容易,从而使系统更稳定、更易于维护。

In this case, as a previous poster mentioned, it allows the image to do other things as well, such as display a progress indicator.

在这种情况下,正如前面提到的海报,它允许图像做其他事情,比如显示进度指示器。