我有一个分别列出 socket 数量的 UItableView,每个 socket 都有一个 Logo 图像。我将这些图像本地保存在 iPhone 上,因此如果在 iPhone 上找到任何图像,它将从那里获取,否则它将发送服务调用并获取数据。它正在相应地工作,但是当我在 tableview 上向下滚动时,它在下载图像时的某个点被挂起,因为它被下载它再次工作正常。有没有什么解决方案可以让我执行下载图像的多任务处理以及通过服务调用或在本地获取图像来表示它们。
这是我的代码..
//本地下载网点图片
NSLog(@"Downloading...");
NSString *imageLink = [NSString stringWithFormat:@"http://cf.abc.pk/outlets/l/%@",outs.logo];
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageLink]]];
NSLog(@"%f,%f",image.size.width,image.size.height);
NSString *docDir =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSLog(@"%@",docDir);
NSLog(@"saving png");
NSString *pngFilePath = [NSString stringWithFormat:@"%@/%d.png",docDir,[[arrOutletIds objectAtIndex:webserviceCall] intValue]];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:pngFilePath];
if(!fileExists)
{
NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(image)];
[data1 writeToFile:pngFilePath atomically:YES];
}
最佳答案
让我们试试:
//When you download something, execute in background thread
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0),
^{
//download some thing
dispatch_async( dispatch_get_main_queue(), ^{
// when download finish execute in main thread to update data
// this function should be called in response of downloading
});
});
希望对你有帮助。
关于ios - 在 iOS sdk 中下载图片的多任务处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23191775/