ios - 为什么所有异步图像加载(SDWebImage、AFNetworking)库都使用 UIImageView 而不是 UIImage?

标签 ios objective-c uiimageview afnetworking sdwebimage

我错过了什么吗?不允许它与 UIImage 一起使用比 UIImageView 更通用吗?您可以将 UIImage 传递给其他人并将其用于不同的用途,其中 UIImageView 实际上只能添加到 View 中。

我最初的想法是,如果您将它交给另一个类并且 UIImage 尚未加载,它将收到 nil 并且不会传递任何新内容。但是如果您传递 UIImageView 也同样适用,不是吗?

最佳答案

AF网络

你是对的,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.
                     }
                 }];

异步加载

What if you hand it to another class and the UIImage hasn't loaded yet?

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

关于ios - 为什么所有异步图像加载(SDWebImage、AFNetworking)库都使用 UIImageView 而不是 UIImage?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21844098/

相关文章:

ios - iOS "WebRTC>Allow Media Capture on Insecure Sites"?

objective-c - 旋转 CAShapeLayer 也会移动位置

objective-c - 以编程方式查找 Dock 的位置?

ios - Swift - 导入后图像质量下降

ios - 查询sqlite数据库时内存分配不断增加

iOS 将 Restful Web 服务的结果保存到 Core Data 的标准方法是什么?

objective-c - 数组枚举期间应用程序崩溃

objective-c - Objective-C 有观点吗?

iOS 在启用缩放(捏合和双击)的情况下全屏显示 UIImage

ios - UIImageView 重新调整大小并同时旋转 UIImageView?