SDWebImage 下载后不显示(仅在重新打开 VController 后)
SDWebImage Not showing after download (only after reopening VController)
我正在尝试使用以下方法加载 3 张图像。问题是这样的,我的完成块没有收到任何错误,所有 3 个图像仅在我关闭 detailedViewController 并重新打开它后才显示。
我错过了什么?感谢您的帮助,非常感谢。
- (UIImage*)loadImageFav1:(NSString *)nameEmail
{
UIImageView *img =[[UIImageView alloc]init];
// NSString *strBack = [NSString stringWithFormat:@"%@%@%@", self.urlPrefix,[self.selectedProfile objectForKey:nameEmailPro],@"_B.png"];
NSString *strProfi = [NSString stringWithFormat:@"%@%@%@", self.urlPrefix,nameEmail,@"_fav1.png"];
//
// NSURL *bkgUrl = [NSURL URLWithString:[strBack stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURL *f1Url = [NSURL URLWithString:[strProfi stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
// // set imges below
NSLog(@"selected fprofile ......str data...%@",strProfi);
////////strt
weakImageViewF1= self.friendImageView1;
[img sd_setImageWithURL:f1Url
placeholderImage:[UIImage imageNamed:@"profile-1.jpg"]
options:SDWebImageRetryFailed
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
NSLog(@"error...back..log %@", error);
if (image) {
// Update model to say that the image was downloaded
NSLog(@"Complete...Background...SSSSSS");
weakImageViewF1.image = image;
[weakImageViewF1 setNeedsLayout];
}
}];
//////
// Test simple call
// [img sd_setImageWithURL:f1Url placeholderImage:[UIImage imageNamed:@"profile-1.jpg"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
// if(error){
// NSLog(@"Callback Error:%@",error);
// }
// if(image){
// NSLog(@"Callback Image:%@",image);
// }
// }];
return img.image;
}
调用 ViewDidAppear 中的所有 3 个图像
if ([self loadImageFav1:self.emailID] != nil) {
// call the same method on a background thread
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UIImage *tmpF1 = [self loadImageFav1:self.emailID];
UIImage *tmpF2 = [self loadImageFav2:self.emailID];
UIImage *tmpF3 = [self loadImageFav3:self.emailID];
// update UI on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
self.friendImageView1.image = tmpF1;
self.friendImageView2.image = tmpF2;
self.friendImageView3.image = tmpF3;
});
});
}
///// 可以使用,但希望得到解释 - 谢谢
所以为了让它工作,我直接将 sdWebImageWithURL 设置为我的原始 ImageView 并停止使用临时 UIImageView *img = [[UIimageVew alloc]init];
我来自:
[img sd_setImageWithURL:f1Url placeholderImage:[UIIm...
收件人:
[self.friendImageView1 sd_setImageWithURL:f1Url placeholderImage:[UIIm...
这是一个并发问题。 SDWebImage 已经是异步的,所以你通过自己的手动异步线程把它搞砸了。
这是你的意思:
- 在新的后台线程(线程 A)中,开始下载图像。
- 这个新的后台线程(线程 A)正在启动它自己的后台线程(线程 B,SDWebImage 正在为您执行此操作)。
- 您的后台线程 A 结束。
- 您的
dispatch_get_main_queue
部分中的代码已运行,但线程 B 仍在下载这些图像,因此它们尚不可用。
它们下次出现的原因是因为 SDWebImage 已经在第一次尝试时缓存了它们。
要解决此问题,您应该将 dispatch_get_main_queue
部分中运行的代码移动到 SDWebImage 调用的 completed
块中。
编辑:
您的更改得到修复的原因是因为 SDWebImage 负责将 UIImage
加载到您首先提供的 UIImageView
中。所以你最初说的是 "load this image into my UIImageView
named img
and also assign that image to my other UIImageView
named self.friendImageView1
",但当时分配给 img
的当前图像是占位符图像,因为来自互联网的图像尚未下载。就像我说的那样,SDWebImage 会在后台线程中自动为您进行下载,下载完成后,它会将下载的图像分配给您正在调用它的 UIImageView
。所以基本上你的原始代码只是添加了一个额外的中间人。
我正在尝试使用以下方法加载 3 张图像。问题是这样的,我的完成块没有收到任何错误,所有 3 个图像仅在我关闭 detailedViewController 并重新打开它后才显示。
我错过了什么?感谢您的帮助,非常感谢。
- (UIImage*)loadImageFav1:(NSString *)nameEmail
{
UIImageView *img =[[UIImageView alloc]init];
// NSString *strBack = [NSString stringWithFormat:@"%@%@%@", self.urlPrefix,[self.selectedProfile objectForKey:nameEmailPro],@"_B.png"];
NSString *strProfi = [NSString stringWithFormat:@"%@%@%@", self.urlPrefix,nameEmail,@"_fav1.png"];
//
// NSURL *bkgUrl = [NSURL URLWithString:[strBack stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURL *f1Url = [NSURL URLWithString:[strProfi stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
// // set imges below
NSLog(@"selected fprofile ......str data...%@",strProfi);
////////strt
weakImageViewF1= self.friendImageView1;
[img sd_setImageWithURL:f1Url
placeholderImage:[UIImage imageNamed:@"profile-1.jpg"]
options:SDWebImageRetryFailed
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
NSLog(@"error...back..log %@", error);
if (image) {
// Update model to say that the image was downloaded
NSLog(@"Complete...Background...SSSSSS");
weakImageViewF1.image = image;
[weakImageViewF1 setNeedsLayout];
}
}];
//////
// Test simple call
// [img sd_setImageWithURL:f1Url placeholderImage:[UIImage imageNamed:@"profile-1.jpg"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
// if(error){
// NSLog(@"Callback Error:%@",error);
// }
// if(image){
// NSLog(@"Callback Image:%@",image);
// }
// }];
return img.image;
}
调用 ViewDidAppear 中的所有 3 个图像
if ([self loadImageFav1:self.emailID] != nil) {
// call the same method on a background thread
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UIImage *tmpF1 = [self loadImageFav1:self.emailID];
UIImage *tmpF2 = [self loadImageFav2:self.emailID];
UIImage *tmpF3 = [self loadImageFav3:self.emailID];
// update UI on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
self.friendImageView1.image = tmpF1;
self.friendImageView2.image = tmpF2;
self.friendImageView3.image = tmpF3;
});
});
}
///// 可以使用,但希望得到解释 - 谢谢
所以为了让它工作,我直接将 sdWebImageWithURL 设置为我的原始 ImageView 并停止使用临时 UIImageView *img = [[UIimageVew alloc]init];
我来自:
[img sd_setImageWithURL:f1Url placeholderImage:[UIIm...
收件人:
[self.friendImageView1 sd_setImageWithURL:f1Url placeholderImage:[UIIm...
这是一个并发问题。 SDWebImage 已经是异步的,所以你通过自己的手动异步线程把它搞砸了。
这是你的意思:
- 在新的后台线程(线程 A)中,开始下载图像。
- 这个新的后台线程(线程 A)正在启动它自己的后台线程(线程 B,SDWebImage 正在为您执行此操作)。
- 您的后台线程 A 结束。
- 您的
dispatch_get_main_queue
部分中的代码已运行,但线程 B 仍在下载这些图像,因此它们尚不可用。
它们下次出现的原因是因为 SDWebImage 已经在第一次尝试时缓存了它们。
要解决此问题,您应该将 dispatch_get_main_queue
部分中运行的代码移动到 SDWebImage 调用的 completed
块中。
编辑:
您的更改得到修复的原因是因为 SDWebImage 负责将 UIImage
加载到您首先提供的 UIImageView
中。所以你最初说的是 "load this image into my UIImageView
named img
and also assign that image to my other UIImageView
named self.friendImageView1
",但当时分配给 img
的当前图像是占位符图像,因为来自互联网的图像尚未下载。就像我说的那样,SDWebImage 会在后台线程中自动为您进行下载,下载完成后,它会将下载的图像分配给您正在调用它的 UIImageView
。所以基本上你的原始代码只是添加了一个额外的中间人。