我一直在使用AFNetworking下载带有占位符的UIImage异步,它可以正常使用JPEG和PNG,但我使用library作为我的GIF图片。当我同步进行时,它运行良好。但是,我想使用AFNetworking在异步中下载图像,同时在ImageView中显示占位符图像。我怎样才能做到这一点?谢谢!
以下是我的代码:
- (void) downloadAllImagesForComment
{
if (_imageLinkList.count > 0)
{
if (images == nil)
images = [[NSMutableDictionary alloc] init];
for (NSDictionary *URL in _imageLinkList) {
if ([images objectForKey:[URL objectForKey:@"link"]] == nil)
{
/*
* imageType = 0 <= other
* imageType = 1 <= gif
*/
int imageType = 0;
NSURL *url = [NSURL URLWithString:[URL objectForKey:@"link"]];
NSError *error = [[NSError alloc] init];
NSLog(@"Retrieving imageData...");
NSData *imageData = [NSData dataWithContentsOfURL:url options:NSDataReadingMappedIfSafe error:&error];
NSLog(@"Retrived imageData.");
uint8_t c;
[imageData getBytes:&c length:1];
switch (c) {
case 0xFF:
NSLog(@"image/jpeg");
imageType = 0;
break;
case 0x89:
NSLog(@"image/png");
imageType = 0;
break;
case 0x47:
NSLog(@"image/gif");
imageType = 1;
break;
case 0x49:
imageType = 0;
break;
case 0x4D:
NSLog(@"image/tiff");
imageType = 0;
break;
}
UIImageView *temp_imageView;
if (imageType == 1)
{
// GIF IMAGE SHOULD BE INITIALIZED HERE!!!!!!
// image = [UIImage animatedImageWithAnimatedGIFData:imageData];
}
else
{
temp_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 280, 250)];
[temp_imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"patch_la"]];
}
[images setValue:temp_imageView forKey:[URL objectForKey:@"link"]];
NSLog(@"Done downloading image: %@", [URL objectForKey:@"link"]);
}
}
}
}
答案 0 :(得分:0)
我建议使用SDWebImage library。它内置了异步动画Gif支持。
包含库后,它很简单(只需将NSURL指向远程Gif):
[imageView setImageWithURL:[NSURL] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType){}];