为什么所有的异步图像加载(SDWebImage,AFNetworking)库都使用UIImageView而不是UIIma



我错过了什么吗?难道不允许它与UIImage一起使用比UIImageView更通用吗?你可以把UIImage交给你,把它用于不同的事情,其中UIImageView实际上只能添加到视图中。

我最初的想法是,如果你把它交给另一个类,而UIImage还没有加载,它会收到nil并且不会传递任何新的东西。但是,如果您通过UIImageView,则同样适用,不是吗?

AFNetworking

你是对的,AFNetworking类别实现(UIImageView+AFNetworking)是基本的。

但是您缺少 AFImageResponseSerializer,它可以验证和解码图像,并提供一些处理图像的选项。 (在旧的 AFNetworking 1.x 版本中,此功能位于 AFImageRequestOperation 中。

SDWebImage

同样,您只查看SDWebImage的非常基本的实现。 这里有很多选项,所以我建议你复习一些其他类,但在最基本的层面上,你可以得到这样的 UIImage:

SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadWithURL:imageURL
                 options:kNilOptions
                 progress:^(NSUInteger receivedSize, NSUInteger expectedSize)
                 {
                     // update a progress view
                 }
                 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished)
                 {
                     if (image)
                     {
                         // Do something with the image; cacheType tells you if it was cached or not.
                     }
                 }];

异步加载

如果您将其交给另一个职业并且UIImage尚未加载怎么办?

好吧,它都是异步加载的,因此在加载 UIImage 之前不会调用完成块。

最新更新