为什么所有异步图像加载(SDWebImage,AFNetworking)库都使用UIImageView而不是UIImage? [英] Why do all the async image loading (SDWebImage, AFNetworking) libraries use UIImageView instead of UIImage?

查看:105
本文介绍了为什么所有异步图像加载(SDWebImage,AFNetworking)库都使用UIImageView而不是UIImage?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想念什么吗?不允许它与UIImage一起使用,而不仅仅是UIImageView?您可以将UIImage交给其他人使用,而UIImageView实际上只能添加到视图中。

Am I missing something? Wouldn't allow it to be used with UIImage be more versatile than just UIImageView? You could hand the UIImage around and use it for different things, where a UIImageView can really only be added to the view.

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

My initial thought was what if you hand it to another class and the UIImage hasn't loaded yet, it would receive nil and not be passed anything new. But the same applies if you pass a UIImageView, wouldn't it?

推荐答案

AFNetworking



您是对的,AFNetworking类别实现(UIImageView + AFNetworking)简直就是白手起家。

AFNetworking

You're right, the AFNetworking category implementation (UIImageView+AFNetworking) is bare-bones.

但是您错过了 AFImageResponseSerializer ,它可以对图像进行验证和解码,并提供一些处理图像的选项。 (在较早的AFNetworking 1.x版本中,此功能位于 AFImageRequestOperation 。)

But you're missing AFImageResponseSerializer, which validates and decodes images and provides a few options for handling them. (In the old 1.x version of AFNetworking, this functionality is in AFImageRequestOperation.)

类似地,您只是在看SDWebImage的最基本实现。这里有很多选项,所以我建议您复习其他一些类,但是在最基本的级别上,您可以像这样获得UIImage:

Similarly, you're only looking at the very basic implementation of SDWebImage. There are a LOT of options here, so I suggest you review some of the other classes, but at the most basic level, you can get a UIImage like this:

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.
                     }
                 }];



异步加载



Asynchronous loading


如果将其交给另一个类怎么办,而UIImage尚未加载?

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

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

Well, it's all loaded asynchronously, so the completion block wouldn't get called until the UIImage was loaded.

这篇关于为什么所有异步图像加载(SDWebImage,AFNetworking)库都使用UIImageView而不是UIImage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆