我错过了什么吗?不允许它与 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/