我一直在使用 AFNetworking 与占位符异步下载 UIImage,它适用于 JPEG 和 PNG,但是我正在为我的 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"]);
}
}
}
}