【问题标题】:Why do all the async image loading (SDWebImage, AFNetworking) libraries use UIImageView instead of UIImage?为什么所有异步图像加载(SDWebImage、AFNetworking)库都使用 UIImageView 而不是 UIImage?
【发布时间】:2014-02-18 04:03:02
【问题描述】:

我错过了什么吗?不允许它与 UIImage 一起使用而不仅仅是 UIImageView 更通用吗?您可以将 UIImage 交给周围并将其用于不同的事情,其中​​ UIImageView 真的只能添加到视图中。

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

【问题讨论】:

    标签: ios objective-c uiimageview afnetworking sdwebimage


    【解决方案1】:

    AFNetworking

    你说得对,AFNetworking 类别的实现(UIImageView+AFNetworking)是准系统。

    但是您缺少AFImageResponseSerializer,它可以验证和解码图像并提供一些处理它们的选项。 (在旧 1.x 版本的 AFNetworking 中,此功能位于 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 被加载之前,完成块不会被调用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-04
      • 2013-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多