【发布时间】:2014-06-05 08:40:20
【问题描述】:
我正在尝试在我的应用程序上设置图像的异步下载。 我按照this issue 中的建议使用SDWebImage。
我在progress和completed方法上设置了断点,一切正常。它运行良好,但我的逻辑直接产生了另一个问题。我不知道如何在我的UIImageView 上异步设置我的图像。
一切都是动态的,每个图像都是独立调用的
这是我的代码的一部分:
[myMenu->_userAvatar setImage:[[CacheCache sharedInstance] getUIImageFromPath:currentUser.avatarPhoto.avatarURL]];
注意CacheCache是我自己的缓存方法。
NSURL* myURL=[NSURL URLWithString:path];
//NSData* myData=[NSData dataWithContentsOfURL:myURL];
NSData* myData;
[SDWebImageDownloader.sharedDownloader downloadImageWithURL:myURL
options:0
progress:^(NSInteger receivedSize, NSInteger expectedSize)
{
DDLogInfo(@"Downloading...");
// progression tracking code
}
completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished)
{
if (image && finished)
{
DDLogInfo(@"Downloaded !");
image = [[UIImage alloc] initWithData:myData];
}
}];
...
return image;
感谢您的帮助。
【问题讨论】:
-
完成块返回图片,完成块前最后一个返回方法会被调用
-
我试图从完成块返回图像,但出现以下错误
Control may reach end of a non-void block。接缝完全正常,因为根据文档downloadImageWithURL:options:progress:completed无法返回UIImageView:- (id <SDWebImageOperation>)downloadImageWithURL:(NSURL *)url options:(SDWebImageDownloaderOptions)options progress:(void (^)(NSInteger, NSInteger))progressBlock completed:(void (^)(UIImage *, NSData *, NSError *, BOOL))completedBlock -
我搜索了这个,发现 downloadImageWithURL 没有任何返回类型(我的错)。我发现的是“downloadImageWithURL”方法,您可以使用该方法制作可以下载图像并设置为图像的自定义视图。例如(UIImageView+WebCache.h)。
-
对于您的情况,我认为“setImageWithURL”方法会起作用,让 SDWebCache 为您缓存图像。
标签: ios objective-c asynchronous uiimageview sdwebimage