如何在UIImageView中加载并显示小尺寸图像后加载大尺寸图像?
问题描述:
我可以使用SDWebImage加载一个带有URL的小尺寸图像,以显示在UIImageView上。但我不知道如何加载一个更大的图像与另一个URL来显示在同一个UIImageView后,我已经加载了较小的大小的图像。如何在UIImageView中加载并显示小尺寸图像后加载大尺寸图像?
[aImageView setImageWithURL:fristUrl placeholderImage:[UIImage imageNamed:@"placeholder.png"] success:^(UIImage *image) {
[aImageView setImageWithURL:secondUrl placeholderImage:image success:^(UIImage *image) {
;
} failure:^(NSError *error) {
NSLog(@"error105size:%@", [error description]);
}];
} failure:^(NSError *error) {
NSLog(@"error50size:%@", [error description]);
}];
我已经使用了上面的代码,但它崩溃了。 希望你的回答。
答
其实,你并不需要创建任何隐藏UIImageView
这样的伎俩。
您必须做的是将第一个URL(使用较小的图像)直接下载到您的UIImageView
中,然后使用SDWebImageManager
在后台下载较大的版本。完成下载后,只需在图像视图中设置下载的图像。
这里是你如何能做到这一点:
// First let the smaller image download naturally
[self.imageView setImageWithURL:self.imageURL];
// Slowly download the larger version of the image
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadWithURL:self.currentPhoto.largeImageURL options:SDWebImageLowPriority progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) {
if (image) {
[self.imageView setImage:image];
}
}];
注意我如何使用SDWebImageLowPriority
选项。这样,图像(应该比第一个图像自然大)将以低优先级下载,并且不应取消第一次下载。
答
UIButton *btn = [[UIButton alloc] init];
[btn sd_setImageWithURL:[NSURL URLWithString:obj.thumbnail_pic] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"photoDefault.png"] options:SDWebImageProgressiveDownload completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
NSLog(@"img samll download ok:%@",obj.thumbnail_pic);
if (image) {
[btn sd_setImageWithURL: [NSURL URLWithString:[ctl getBigImg:obj.thumbnail_pic]] forState:UIControlStateNormal
placeholderImage:image];
}
}];